Esempio n. 1
0
            public void FromSynapsis(Synapsis s)
            {
                this.FromLayerName   = s.From.Layer.Name;
                this.FromNeuronIndex = s.From.Index;

                this.ToLayerName   = s.To.Layer.Name;
                this.ToNeuronIndex = s.To.Index;

                this.Weight = s.Weight;
            }
Esempio n. 2
0
            public SerializableSynapsis(Synapsis s)
            {
                this.FromLayerName   = s.From.Layer.Name;
                this.FromNeuronIndex = s.From.Index;

                this.ToLayerName   = s.To.Layer.Name;
                this.ToNeuronIndex = s.To.Index;

                this.Weight = s.Weight;
            }
Esempio n. 3
0
 /// <summary>
 /// Adds a synapsis between neurons, allows to create feedback and self feedback loop
 /// </summary>
 /// <param name="synapsis">The synapsis to add</param>
 public void AddSynapsis(Synapsis synapsis)
 {
     this.Synapses.Add(synapsis);
     if (!synapsis.From.PostSynapses.Contains(synapsis))
     {
         synapsis.From.PostSynapses.Add(synapsis);
     }
     if (!synapsis.To.PreSynapses.Contains(synapsis))
     {
         synapsis.To.PreSynapses.Add(synapsis);
     }
 }
Esempio n. 4
0
        /// <summary>
        /// Creates Synapses between neurons of neignboring layers
        /// </summary>
        public void CreateSynapses()
        {
            this.Synapses.Clear();
            Layer current = this._InputLayer;

            while (current != this._OutputLayer)
            {
                foreach (Neuron from in current.Neurons)
                {
                    foreach (Neuron to in current.NextLayer.Neurons)
                    {
                        Synapsis synapsis = new Synapsis();
                        synapsis.From = from;
                        synapsis.To   = to;
                        from.PostSynapses.Add(synapsis);
                        to.PreSynapses.Add(synapsis);
                        this.Synapses.Add(synapsis);
                    }
                }
                current = current.NextLayer;
            }
        }
Esempio n. 5
0
 public bool RemoveSynapsis(Synapsis synapsis)
 {
     return(this.Synapses.Remove(synapsis));
 }
Esempio n. 6
0
        /// <summary>
        /// Deserialization constructor
        /// </summary>
        /// <param name="info"></param>
        /// <param name="sc"></param>
        public NeuralNetwork(SerializationInfo info, StreamingContext sc)
        {
            Layer         inputlayer     = (Layer)info.GetValue("InputLayer", typeof(Layer));
            Layer         outputlayer    = (Layer)info.GetValue("OutputLayer", typeof(Layer));
            List <Layer>  hiddenlayers   = (List <Layer>)info.GetValue("HiddenLayers", typeof(List <Layer>));
            List <string> layerhierarchy = (List <string>)info.GetValue("Layer hierarchy", typeof(List <string>));

            this.InputLayer = new Layer(this, LayerType.Input);
            this.InputLayer.NumberOfNeurons = inputlayer.NumberOfNeurons;
            for (int i = 0; i < inputlayer.NumberOfNeurons; i++)
            {
                this.InputLayer.Neurons[i].Threshold = inputlayer.Neurons[i].Threshold;
            }
            this.InputLayer.ActivationFunction       = inputlayer.ActivationFunction;
            this.InputLayer.InputCombinationFunction = inputlayer.InputCombinationFunction;
            this.OutputLayer = new Layer(this, LayerType.Output);
            this.OutputLayer.NumberOfNeurons = outputlayer.NumberOfNeurons;
            for (int i = 0; i < outputlayer.NumberOfNeurons; i++)
            {
                this.OutputLayer.Neurons[i].Threshold = outputlayer.Neurons[i].Threshold;
            }
            this.OutputLayer.ActivationFunction       = outputlayer.ActivationFunction;
            this.OutputLayer.InputCombinationFunction = outputlayer.InputCombinationFunction;
            for (int j = 0; j < hiddenlayers.Count; j++)
            {
                Layer hiddenlayer = new Layer(this, LayerType.Hidden);
                hiddenlayer.NumberOfNeurons = hiddenlayers[j].NumberOfNeurons;
                for (int i = 0; i < hiddenlayers[j].NumberOfNeurons; i++)
                {
                    hiddenlayer.Neurons[i].Threshold = hiddenlayers[j].Neurons[i].Threshold;
                }
                hiddenlayer.ActivationFunction       = hiddenlayers[j].ActivationFunction;
                hiddenlayer.InputCombinationFunction = hiddenlayers[j].InputCombinationFunction;
                this.HiddenLayers.Add(hiddenlayer);
            }

            //Restore the layer hierarchy
            Layer current = this.GetLayer(layerhierarchy[0]);;

            for (int i = 1; i < layerhierarchy.Count; i++)
            {
                if (current != null)
                {
                    Layer next = this.GetLayer(layerhierarchy[i]);
                    if (next != null)
                    {
                        current.NextLayer  = next;
                        next.PreviousLayer = current;
                        current            = next;
                    }
                }
                else
                {
                    current = this.GetLayer(layerhierarchy[i]);
                }
            }

            //Load synapses
            List <SerializableSynapsis> ss = (List <SerializableSynapsis>)info.GetValue("Synapses", typeof(List <SerializableSynapsis>));

            this.Synapses.Clear();
            for (int i = 0; i < ss.Count; i++)
            {
                try
                {
                    Synapsis s = new Synapsis();
                    s.From   = this.GetLayer(ss[i].FromLayerName).Neurons[ss[i].FromNeuronIndex];
                    s.To     = this.GetLayer(ss[i].ToLayerName).Neurons[ss[i].ToNeuronIndex];
                    s.Weight = ss[i].Weight;
                    if (s.From != null && s.To != null)
                    {
                        Synapses.Add(s);
                    }
                }
                catch
                {
                }
            }
        }