Пример #1
0
        public Simulation CreateSimulation()
        {
            var            activationFunction = CreateActivationFunction();
            var            neuron             = CreateNeuron(activationFunction);
            INeuralNetwork neuralNetwork      = null;

            if (Network == "NeuralNetwork")
            {
                var weightInitializer = new WeightInitializer(Weights.Min, Weights.Max);
                neuralNetwork = new NeuralNetwork(neuron, Inputs, Outputs, weightInitializer, HiddenNeurons);
            }
            else if (Network == "SOMNetwork")
            {
                neuralNetwork = new SOMNetwork(neuron, Inputs, Outputs, MaxEpoch);
            }

            var simulation = new Simulation(neuralNetwork, true);

            simulation.ValidationData = ValidationData;
            simulation.ImagesDisturbanceProbability  = ImagesDisturbanceProbability;
            simulation.ImageDisturbanceMaxDifference = ImageDisturbanceMaxDifference;
            simulation.MaxEpoch = MaxEpoch;
            simulation.Config   = this;
            return(simulation);
        }
Пример #2
0
        public NeuralModelBase()
        {
            synapseInnovNbTracker = new SynapseInnovNbTracker();

            Neurons  = new Dictionary <InnovationNumber, Neuron>();
            Synapses = new Dictionary <Synapse, WeightInitializer>();

            this.defaultWeightInitializer = () => GARandomManager.NextFloat(-1f, 1f);
        }
Пример #3
0
 public Synapse AddConnection(
     Neuron startNeuron,
     Neuron endNeuron,
     WeightInitializer weightInitializer = null)
 {
     return(AddConnection(
                startNeuron.InnovationNb,
                endNeuron.InnovationNb,
                weightInitializer
                ));
 }
Пример #4
0
        public void ConnectBias(
            BiasNeuron bias,
            IEnumerable <IEnumerable <Neuron> > layers,
            WeightInitializer weightInitializer = null)
        {
            var biasLayer = new[] { bias };

            foreach (var layer in layers)
            {
                ConnectNeurons(biasLayer, layer, weightInitializer).ToArray();
            }
        }
Пример #5
0
        public void ConnectLayers(
            IEnumerable <IEnumerable <Neuron> > layers,
            WeightInitializer weightInitializer = null)
        {
            var prevLayer = layers.First();

            foreach (var layer in layers.Skip(1))
            {
                ConnectNeurons(prevLayer, layer, weightInitializer).ToArray();
                prevLayer = layer;
            }
        }
Пример #6
0
 public IEnumerable <Synapse> ConnectNeurons(
     IEnumerable <Neuron> group1,
     IEnumerable <Neuron> group2,
     WeightInitializer weightInitializer = null)
 {
     foreach (var neuron1 in group1)
     {
         foreach (var neuron2 in group2)
         {
             yield return(AddConnection(
                              neuron1.InnovationNb,
                              neuron2.InnovationNb,
                              weightInitializer ?? defaultWeightInitializer));
         }
     }
 }
Пример #7
0
        public Synapse AddConnection(
            InnovationNumber startNeuron,
            InnovationNumber endNeuron,
            WeightInitializer weightInitializer = null)
        {
            if (!Neurons.ContainsKey(startNeuron) || !Neurons.ContainsKey(endNeuron))
            {
                throw new Exception("The given neurons are not yer registered.");
            }

            var innov  = synapseInnovNbTracker.GetHystoricalMark(startNeuron, endNeuron);
            var result = new Synapse(innov, 0, startNeuron, endNeuron)
            {
                weightConstraints = this.WeightConstraints
            };

            if (weightInitializer == null)
            {
                weightInitializer = defaultWeightInitializer;
            }
            Synapses.Add(result, weightInitializer);

            return(result);
        }
Пример #8
0
        public static void AddLSTM(
            this NeuralModelBase model,
            out Neuron input,
            out Neuron output,
            BiasNeuron biasNeuron = null,
            WeightInitializer weightInitializer = null,
            string groupName = "LSTM")
        {
            var concatNeur = model.AddNeuron(
                sampleNeuron: new Neuron(-1, null)
                );

            // Multiply Gate
            var sigmoid1 = model.AddNeuron(
                sampleNeuron: new Neuron(-1, ActivationFunctions.Sigmoid)
                );

            model.AddConnection(concatNeur, sigmoid1, weightInitializer)
            .isTransferConnection = true;

            var multiplyGate = model.AddNeuron(
                sampleNeuron: new Neuron(-1, null)
            {
                ValueCollector = new MultValueCollector()
            }
                );

            model.AddConnection(sigmoid1, multiplyGate, weightInitializer);

            // Addition gate
            var sigmoid2 = model.AddNeuron(
                sampleNeuron: new Neuron(-1, ActivationFunctions.Sigmoid)
                );

            model.AddConnection(concatNeur, sigmoid2, weightInitializer)
            .isTransferConnection = true;

            var tanh = model.AddNeuron(
                sampleNeuron: new Neuron(-1, ActivationFunctions.TanH)
                );

            model.AddConnection(concatNeur, tanh, weightInitializer)
            .isTransferConnection = true;

            var sigmoidAndTanhMultGate = model.AddNeuron(
                sampleNeuron: new Neuron(-1, null)
            {
                ValueCollector = new MultValueCollector()
            }
                );

            model.AddConnection(sigmoid2, sigmoidAndTanhMultGate, weightInitializer);
            model.AddConnection(tanh, sigmoidAndTanhMultGate, weightInitializer);

            var additionGate = model.AddNeuron(new Neuron(-1, null));

            model.AddConnection(multiplyGate, additionGate, weightInitializer)
            .isTransferConnection = true;
            model.AddConnection(sigmoidAndTanhMultGate, additionGate, weightInitializer)
            .isTransferConnection = true;

            // Tanh gate
            var sigmoid3 = model.AddNeuron(
                sampleNeuron: new Neuron(-1, ActivationFunctions.Sigmoid)
                );

            model.AddConnection(concatNeur, sigmoid3, weightInitializer)
            .isTransferConnection = true;

            var finalMult = model.AddNeuron(
                sampleNeuron: new Neuron(-1, null)
            {
                ValueCollector = new MultValueCollector()
            }
                );

            var tanhGate = model.AddNeuron(
                sampleNeuron: new Neuron(-1, ActivationFunctions.TanH)
                );

            model.AddConnection(additionGate, tanhGate, weightInitializer)
            .isTransferConnection = true;

            model.AddConnection(sigmoid3, finalMult, weightInitializer);
            model.AddConnection(tanhGate, finalMult, weightInitializer)
            .isTransferConnection = true;

            // Adding memory neurons
            var finalMultMem = model.AddNeuron(
                sampleNeuron: new MemoryNeuron(-1, finalMult.InnovationNb)
                );

            model.AddConnection(finalMultMem, concatNeur, weightInitializer)
            .isTransferConnection = true;

            var cellStateMem = model.AddNeuron(
                sampleNeuron: new MemoryNeuron(-1, additionGate.InnovationNb)
                );

            model.AddConnection(cellStateMem, multiplyGate, weightInitializer)
            .isTransferConnection = true;

            // Connecting bias
            if (biasNeuron != null)
            {
                model.AddConnection(biasNeuron, sigmoid1);
                model.AddConnection(biasNeuron, sigmoid2);
                model.AddConnection(biasNeuron, tanh);
                model.AddConnection(biasNeuron, sigmoid3);
            }

            // Assign neuron group
            concatNeur.group = groupName;
            sigmoid1.group   = groupName;
            sigmoid2.group   = groupName;
            sigmoid3.group   = groupName;
            tanh.group       = groupName;

            additionGate.group = groupName;
            multiplyGate.group = groupName;
            tanhGate.group     = groupName;

            sigmoidAndTanhMultGate.group = groupName;
            finalMult.group = groupName;

            cellStateMem.group = groupName;
            finalMultMem.group = groupName;

            // Assigning out's
            input  = concatNeur;
            output = finalMult;
        }
Пример #9
0
 public void SetUp()
 {
     _weightSetterMock  = new Mock <IWeightSetter>();
     _network           = new NetworkBuilder().Setup();
     _weightInitializer = new WeightInitializer(_network.GetNetwork(), weightSetter: _weightSetterMock.Object);
 }