Beispiel #1
0
        public NodeLayer(int Inputs, int Outputs)
        {
            if (Inputs == 0)
                throw new ArgumentException("A NodeLayer must have at least one input");

            if (Outputs == 0)
                throw new ArgumentException("A NodeLayer must have at least one output");

            this.Inputs = Inputs;

            Nodes = new Node[Outputs];
            for (int i = 0; i < Outputs; i++)
            {
                Nodes[i] = new Node(Inputs);
            }
        }
        public void LogicNodeXOR()
        {
            INode node = new Node(2);

            BackPropagation prop = new BackPropagation(2, 1);
            DataSet _1 = new DataSet(new double[] { 0, 0 }, new double[] { 0 });    // 0 | 0 = 0
            DataSet _2 = new DataSet(new double[] { 0, 1 }, new double[] { 1 });    // 0 | 1 = 1
            DataSet _3 = new DataSet(new double[] { 1, 0 }, new double[] { 1 });    // 1 | 0 = 1
            DataSet _4 = new DataSet(new double[] { 1, 1 }, new double[] { 0 });    // 1 | 1 = 0

            prop.AddDataSet(_1);
            prop.AddDataSet(_2);
            prop.AddDataSet(_3);
            prop.AddDataSet(_4);

            int epoch = 1000;
            double SSE = prop.Train(node, epoch);
            // this problem isn't possible for a single node, so check that if fails.
            Assert.IsTrue(SSE > 0.8, "LogicNodeXOR SSE after {0} epochs is '{1}'", epoch, SSE);
        }
        public void LogicNodeOR()
        {
            INode node = new Node(2);

            BackPropagation prop = new BackPropagation(2, 1);
            DataSet _1 = new DataSet(new double[] { 0, 0 }, new double[] { 0 });    // 0 | 0 = 0
            DataSet _2 = new DataSet(new double[] { 0, 1 }, new double[] { 1 });    // 0 | 1 = 1
            DataSet _3 = new DataSet(new double[] { 1, 0 }, new double[] { 1 });    // 1 | 0 = 1
            DataSet _4 = new DataSet(new double[] { 1, 1 }, new double[] { 1 });    // 1 | 1 = 1

            prop.AddDataSet(_1);
            prop.AddDataSet(_2);
            prop.AddDataSet(_3);
            prop.AddDataSet(_4);

            int epoch = 1000;
            double SSE = prop.Train(node, epoch);

            foreach (var dataSet in prop.DataSets)
            {
                double result = node.Calculate(dataSet.Inputs);
                Assert.IsTrue(
                    (dataSet.Outputs[0] == 1.0 && result > 0.8) ||
                (dataSet.Outputs[0] == 0.0 && result < 0.2), "LogicNodeOR failed to learn.");

            }

            Assert.IsTrue(SSE < 0.2, "LogicNodeOR SSE after {0} epochs is '{1}'", epoch, SSE);
        }
        public void LogicNodeAND()
        {
            INode node = new Node(2);

            BackPropagation prop = new BackPropagation(2, 1);
            DataSet _1 = new DataSet(new double[] { 0, 0 }, new double[] { 0 });    // 0 | 0 = 0
            DataSet _2 = new DataSet(new double[] { 0, 1 }, new double[] { 0 });    // 0 | 1 = 0
            DataSet _3 = new DataSet(new double[] { 1, 0 }, new double[] { 0 });    // 1 | 0 = 0
            DataSet _4 = new DataSet(new double[] { 1, 1 }, new double[] { 1 });    // 1 | 1 = 1

            prop.AddDataSet(_1);
            prop.AddDataSet(_2);
            prop.AddDataSet(_3);
            prop.AddDataSet(_4);

            int epoch = 1000;
            double SSE = prop.Train(node, epoch);

            Assert.IsTrue(SSE < 0.2, "LogicNodeAND SSE after {0} epochs is '{1}'", epoch, SSE);
        }