Ejemplo n.º 1
0
        public void ActivateNeuron()
        {
            BiasNeuron bias = new BiasNeuron();
            double     w0   = 0;

            neuron.Connect(bias, w0);

            Assert.Throws(typeof(NotConfiguredException),
                          () => neuron.Activation());
            InputNeuron i1 = new InputNeuron();
            InputNeuron i2 = new InputNeuron();
            InputNeuron i3 = new InputNeuron();

            i1.Input = 1;
            i2.Input = 1;
            i3.Input = 1;

            double w1 = 1;
            double w2 = 1;
            double w3 = 1;

            neuron.Connect(i1, w1);
            neuron.Connect(i2, w2);
            neuron.Connect(i3, w3);
            double tx = i1.Input * w1 + i2.Input * w2 + i3.Input * w3;
            double expected_activation = 1 / (1 + Math.Pow(Math.E, -tx));

            MyAssert.CloseTo(neuron.Activation(), expected_activation);
        }
Ejemplo n.º 2
0
        public void TestXNOR_Manualy()
        {
            Neuron     a3_1   = new Neuron();
            BiasNeuron bias_2 = new BiasNeuron();

            a3_1.Connect(bias_2);
            Neuron a2_1 = new Neuron();

            a3_1.Connect(a2_1);
            Neuron a2_2 = new Neuron();

            a3_1.Connect(a2_2);
            BiasNeuron bias_1 = new BiasNeuron();

            a2_1.Connect(bias_1);
            a2_2.Connect(bias_1);
            InputNeuron a1_1 = new InputNeuron();

            a2_1.Connect(a1_1);
            a2_2.Connect(a1_1);
            InputNeuron a1_2 = new InputNeuron();

            a2_1.Connect(a1_2);
            a2_2.Connect(a1_2);

            a3_1.SetWeight(0, -10);
            a3_1.SetWeight(1, 20);
            a3_1.SetWeight(2, 20);

            a2_1.SetWeight(0, -30);
            a2_1.SetWeight(1, 20);
            a2_1.SetWeight(2, 20);

            a2_2.SetWeight(0, 10);
            a2_2.SetWeight(1, -20);
            a2_2.SetWeight(2, -20);

            a1_1.Input = 0;
            a1_2.Input = 0;
            MyAssert.CloseTo(a3_1.Activation(), 1);

            a1_1.Input = 0;
            a1_2.Input = 1;
            MyAssert.CloseTo(a3_1.Activation(), 0);

            a1_1.Input = 1;
            a1_2.Input = 0;
            MyAssert.CloseTo(a3_1.Activation(), 0);

            a1_1.Input = 1;
            a1_2.Input = 1;
            MyAssert.CloseTo(a3_1.Activation(), 1);
        }
Ejemplo n.º 3
0
        void NeuronActivation()
        {
            Neuron n = SimpleNeuron();

            Assert.That(n.Activation(new List <double>()
            {
                0.2, 0.8, 0.1
            }), Is.InRange(0.67, 0.68));
            Assert.That(n.Activation(new List <double>()
            {
                0.1, 0.1, 0.1
            }), Is.InRange(0.63, 0.64));
        }
Ejemplo n.º 4
0
        public void Neuron_Activation_CorrectLogic()
        {
            // arrange
            Neuron neuron  = new Neuron();
            Sinaps sinaps1 = new Sinaps(new Neuron()
            {
                Data = 0.5
            }, 0.2);
            Sinaps sinaps2 = new Sinaps(new Neuron()
            {
                Data = 0.3
            }, 0.4);

            neuron.Sinapses.Add(sinaps1);
            neuron.Sinapses.Add(sinaps2);
            double expected = 0;

            foreach (Sinaps sinaps in neuron.Sinapses)
            {
                expected += sinaps.GetData();
            }
            expected = (Math.Exp(2 * expected) - 1) / (Math.Exp(2 * expected) + 1);

            // act
            neuron.Activation();

            // assert
            double actual = neuron.Data;

            Assert.AreEqual(expected, actual);
        }
Ejemplo n.º 5
0
        private IEnumerable <BakedOperation> RecursiveOp(
            NeuralGenome genome,
            Neuron target,
            HashSet <InnovationNumber> solvedNeurons)
        {
            if (target.IsStarting)
            {
                solvedNeurons.Add(target.InnovationNb);
                yield break;
            }

            Trace.Assert(!solvedNeurons.Contains(target.InnovationNb));

            solvedNeurons.Add(target.InnovationNb);
            yield return(() => target.Value = target.ValueCollector.InitialValue);

            foreach (var gene in genome.GetGenesToNeuron(target.InnovationNb))
            {
                var synapse = gene.Synapse;
                if (!synapse.enabled)
                {
                    continue;
                }

                if (!solvedNeurons.Contains(synapse.incoming))
                {
                    var neuronToSolve = genome.Neurons[synapse.incoming];
                    foreach (var op in RecursiveOp(genome, neuronToSolve, solvedNeurons))
                    {
                        yield return(op);
                    }
                }

                yield return(() =>
                {
                    var incommingNeurVal = genome.Neurons[synapse.incoming].Value;
                    var newDelta = synapse.Weight * incommingNeurVal;
                    var newVal = target.ValueCollector.Collect(
                        target.Value,
                        newDelta);

                    target.Value = newVal;
                });
            }

            if (target.ValueModifiers != null)
            {
                foreach (var valueModifier in target.ValueModifiers)
                {
                    yield return(() =>
                                 target.Value = (float)valueModifier(target.Value));
                }
            }

            if (target.Activation != null)
            {
                yield return(() =>
                             target.Value = (float)target.Activation(target.Value));
            }
        }
Ejemplo n.º 6
0
        public void Neuron_Activation_CorrectRange()
        {
            // arrange
            Neuron neuron  = new Neuron();
            Sinaps sinaps1 = new Sinaps(new Neuron()
            {
                Data = 0.5
            }, 0.5);
            Sinaps sinaps2 = new Sinaps(new Neuron()
            {
                Data = 0.3
            }, 0.4);

            neuron.Sinapses.Add(sinaps1);
            neuron.Sinapses.Add(sinaps2);

            // act
            neuron.Activation();

            // assert
            double actual = neuron.Data;

            Assert.IsTrue(actual >= -1 && actual <= 1);
        }
Ejemplo n.º 7
0
        public void TestXNOR_Manualy()
        {
            Neuron a3_1 = new Neuron();
            BiasNeuron bias_2 = new BiasNeuron();
            a3_1.Connect(bias_2);
            Neuron a2_1 = new Neuron();
            a3_1.Connect(a2_1);
            Neuron a2_2 = new Neuron();
            a3_1.Connect(a2_2);
            BiasNeuron bias_1 = new BiasNeuron();
            a2_1.Connect(bias_1);
            a2_2.Connect(bias_1);
            InputNeuron a1_1 = new InputNeuron();
            a2_1.Connect(a1_1);
            a2_2.Connect(a1_1);
            InputNeuron a1_2 = new InputNeuron();
            a2_1.Connect(a1_2);
            a2_2.Connect(a1_2);

            a3_1.SetWeight(0, -10);
            a3_1.SetWeight(1, 20);
            a3_1.SetWeight(2, 20);

            a2_1.SetWeight(0, -30);
            a2_1.SetWeight(1, 20);
            a2_1.SetWeight(2, 20);

            a2_2.SetWeight(0, 10);
            a2_2.SetWeight(1, -20);
            a2_2.SetWeight(2, -20);

            a1_1.Input = 0;
            a1_2.Input = 0;
            MyAssert.CloseTo(a3_1.Activation(), 1);

            a1_1.Input = 0;
            a1_2.Input = 1;
            MyAssert.CloseTo(a3_1.Activation(), 0);

            a1_1.Input = 1;
            a1_2.Input = 0;
            MyAssert.CloseTo(a3_1.Activation(), 0);

            a1_1.Input = 1;
            a1_2.Input = 1;
            MyAssert.CloseTo(a3_1.Activation(), 1);
        }
Ejemplo n.º 8
0
 public void Activaction_CorrectValues()
 {
     Assert.AreEqual(0.5, Neuron.Activation(0.0));
     Assert.IsTrue(Neuron.Activation(10.0) > 0.95);
     Assert.IsTrue(Neuron.Activation(-10.0) < 0.05);
 }