Пример #1
0
        public void TestForwardSignal()
        {
            var rand = new Random(123456);

            var NLA = new NeuralLayer(1);
            var NLB = new NeuralLayer(1);

            NLA.SignalReceive = NLB.SignalReceive;
            NLA.Bias[0]       = (float)rand.NextDouble();

            for (int iteration = 0; iteration < 10; iteration++)
            {
                NLA.SignalReceive[0] = (float)rand.NextDouble();

                NLA.ForwardSignal();
                NLB.ForwardSignal();

                NLB.ErrorReceive[0] = NLA.SignalRespond[0] - NLB.SignalRespond[0];

                NLB.FeedBackError();

                NLB.UpdateWeight(0.5f, 0.25f);
            }

            Assert.Equal(NLB.Bias[0], NLA.Bias[0], 3);
        }
Пример #2
0
        public void TestAll()
        {
            int InputCount  = 2;
            int HiddenCount = 10;
            int OutputCount = 1;

            NeuralLayer InputLayer  = new NeuralLayer(InputCount);
            NeuralLayer HiddenLayer = new NeuralLayer(HiddenCount);
            NeuralLayer OutputLayer = new NeuralLayer(OutputCount);

            SynapticLayer SynLayerIH = new SynapticLayer(InputCount, HiddenCount);
            SynapticLayer SynLayerHO = new SynapticLayer(HiddenCount, OutputCount);

            SynLayerIH.SignalReceive = InputLayer.SignalRespond;
            SynLayerIH.SignalRespond = HiddenLayer.SignalReceive;

            SynLayerHO.SignalReceive = HiddenLayer.SignalRespond;
            SynLayerHO.SignalRespond = OutputLayer.SignalReceive;

            SynLayerHO.ErrorReceive = OutputLayer.ErrorRespond;
            SynLayerHO.ErrorRespond = HiddenLayer.ErrorReceive;

            SynLayerIH.ErrorReceive = HiddenLayer.ErrorRespond;
            SynLayerIH.ErrorRespond = InputLayer.ErrorReceive;

            var rand = new Random(123456);

            for (int i = 0; i < InputCount; i++)
            {
                for (int j = 0; j < HiddenCount; j++)
                {
                    SynLayerIH.Weights[j, i] = (float)rand.NextDouble();
                }
            }

            for (int i = 0; i < HiddenCount; i++)
            {
                for (int j = 0; j < OutputCount; j++)
                {
                    SynLayerHO.Weights[j, i] = (float)rand.NextDouble();
                }
            }

            float O0 = 0;

            for (int i = 0; i < 10; i++)
            {
                for (float I0 = -1; I0 < 1.1f; I0 += 2.0f)
                {
                    for (float I1 = -1; I1 < 1.1f; I1 += 2.0f)
                    {
                        O0 = I1 < I0 ? 1 : (I1 > I0 ? 1 : -1);

                        //InputLayer.ForwardSignal();
                        InputLayer.SignalRespond[0] = I0;
                        InputLayer.SignalRespond[1] = I1;

                        SynLayerIH.ForwardSignal();
                        HiddenLayer.ForwardSignal();
                        SynLayerHO.ForwardSignal();
                        OutputLayer.ForwardSignal();

                        OutputLayer.ErrorRespond[0] = OutputLayer.SignalRespond[0] - O0;

                        OutputLayer.FeedBackError();
                        SynLayerHO.FeedBackError();
                        HiddenLayer.FeedBackError();
                        SynLayerIH.FeedBackError();
                        InputLayer.FeedBackError();

                        InputLayer.UpdateWeight(0.1f, 0.1f);
                        SynLayerIH.UpdateWeight(0.1f, 0.1f);
                        HiddenLayer.UpdateWeight(0.1f, 0.1f);
                        SynLayerHO.UpdateWeight(0.1f, 0.1f);
                        OutputLayer.UpdateWeight(0.1f, 0.1f);
                    }
                }
            }
        }