示例#1
0
 //:this(),多态,继承自构造函数,先执行neuron()的构造函数
 public Neuron(IEnumerable <Neuron> inputNeurons) : this()
 {
     foreach (var inputNeuron in inputNeurons)
     {
         var synapse = new Synapse(inputNeuron, this);
         inputNeuron.OutputSynapses.Add(synapse);
         this.InputSynapses.Add(synapse);
     }
 }
示例#2
0
        public static Network ImportNetwork()
        {
            var dn = GetHelperNetwork();

            if (dn == null)
            {
                return(null);
            }

            var network    = new Network();
            var allNeurons = new List <Neuron>();

            network.LearnRate = dn.LearnRate;
            network.Momentum  = dn.Momentum;

            //Input Layer
            foreach (var n in dn.InputLayer)
            {
                var neuron = new Neuron
                {
                    Id          = n.Id,
                    Bias        = n.Bias,
                    BiasDelta   = n.BiasDelta,
                    Gradient    = n.Gradient,
                    InputValue  = n.InputValue,
                    OutputValue = n.OutputValue,
                    Error       = n.Error
                };

                network.InputLayer.Add(neuron);
                allNeurons.Add(neuron);
            }

            //Hidden Layers
            foreach (var layer in dn.HiddenLayers)
            {
                var neurons = new List <Neuron>();
                foreach (var n in layer)
                {
                    var neuron = new Neuron
                    {
                        Id          = n.Id,
                        Bias        = n.Bias,
                        BiasDelta   = n.BiasDelta,
                        Gradient    = n.Gradient,
                        InputValue  = n.InputValue,
                        OutputValue = n.OutputValue,
                        Error       = n.Error
                    };

                    neurons.Add(neuron);
                    allNeurons.Add(neuron);
                }

                network.HiddenLayers.Add(neurons);
            }

            //Export Layer
            foreach (var n in dn.OutputLayer)
            {
                var neuron = new Neuron
                {
                    Id          = n.Id,
                    Bias        = n.Bias,
                    BiasDelta   = n.BiasDelta,
                    Gradient    = n.Gradient,
                    InputValue  = n.InputValue,
                    OutputValue = n.OutputValue,
                    Error       = n.Error
                };

                network.OutputLayer.Add(neuron);
                allNeurons.Add(neuron);
            }

            //Synapses
            foreach (var syn in dn.Synapses)
            {
                var synapse = new Synapse {
                    Id = syn.Id
                };
                var inputNeuron  = allNeurons.First(x => x.Id == syn.InputNeuronId);
                var outputNeuron = allNeurons.First(x => x.Id == syn.OutputNeuronId);
                synapse.InputNeuron  = inputNeuron;
                synapse.OutputNeuron = outputNeuron;
                synapse.Weight       = syn.Weight;
                synapse.WeightDelta  = syn.WeightDelta;

                inputNeuron.OutputSynapses.Add(synapse);
                outputNeuron.InputSynapses.Add(synapse);
            }
            return(network);
        }