Example #1
0
File: Neuron.cs Project: dm-j/nn
        private void AddSynapse(Neuron inputNeuron)
        {
            var synapse = new Synapse(inputNeuron, this);

            inputNeuron.OutputSynapses.Add(synapse);
            InputSynapses.Add(synapse);
        }
        /// <summary>
        /// Connects to input (left) layer
        /// </summary>
        /// <param name="inputNeuron">Neuron form left(previous) layer</param>
        /// <param name="weight">Value of synapse between input and current neuron</param>
        public void ConnectToInputNeuron(Neuron inputNeuron, float weight)
        {
            var synapse = new Synapse(inputNeuron, this, weight);

            InputSynapses.Add(synapse);
            inputNeuron.OutputSynapses.Add(synapse);
        }
Example #3
0
 public double CalculateValue()
 {
     return(Value = _act(
                InputSynapses.Select(
                    synapse => synapse.Weight * synapse.InputNeuron.Value
                    ).Sum() + Bias
                ));
 }
Example #4
0
 public Neuron(IEnumerable <Neuron> inputNeurons) : this()
 {
     foreach (var inputNeuron in inputNeurons)
     {
         var synapse = new Synapse(inputNeuron, this);
         inputNeuron.OutputSynapses.Add(synapse);
         InputSynapses.Add(synapse);
     }
 }
Example #5
0
        public void Initialize(int numberInputs)
        {
            for (int i = 0; i < numberInputs; i++)
            {
                InputSynapses.Add(new Synapse(i + 1, this, Util.r.NextDouble()));
            }

            LearningRate = 1;
        }
Example #6
0
 public Neuron(IEnumerable <Neuron> inputNeurons, Type activationFunctionType = null) : this(activationFunctionType)
 {
     foreach (var inputNeuron in inputNeurons)
     {
         var synapse = new Synapse(inputNeuron, this);
         inputNeuron.OutputSynapses.Add(synapse);
         InputSynapses.Add(synapse);
     }
 }
Example #7
0
 //Tworzenie neuronu wraz z podłączeniem neuronów wejściowych
 public Neuron(List <Neuron> inputNeurons, IActivationFunction activationFunction) : this(activationFunction)
 {
     foreach (var inputNeuron in inputNeurons)
     {
         var synapse = new Synapse(inputNeuron, this);
         inputNeuron.OutputSynapses.Add(synapse);
         InputSynapses.Add(synapse);
     }
 }
Example #8
0
        public double GetOuputValue()
        {
            double sum = 0;

            for (int i = 0; i < InputSynapses.Count; i++)
            {
                sum += InputSynapses[i].Weight * InputSynapses[i].InputNeuron.Output;
            }
            return(Output = Sigmoid.Output(InputSynapses.Sum(a => a.Weight * a.InputNeuron.Output) + BiasWeight));
        }
        /// <summary>
        /// Gets bias connected to current neuron
        /// </summary>
        /// <returns>Connection</returns>
        public Synapse GetInputBiasSynapse()
        {
            if (InputSynapses.Count == 0)
            {
                return(null);
            }

            var biasSynapse = InputSynapses.Skip(InputSynapses.Count - 1).Take(1).Single();

            return(biasSynapse);
        }
        /// <summary>
        /// Get all synapses connected to current neuron
        /// </summary>
        /// <returns>List of synapses</returns>
        public List <Synapse> GetInputWeightSynapses()
        {
            if (InputSynapses.Count == 0)
            {
                return(new List <Synapse>());
            }

            var synapses = InputSynapses.Take(InputSynapses.Count - 1).ToList();

            return(synapses);
        }
Example #11
0
        /// <summary>
        /// Initializes a new instance of the <see cref="Neuron"/> class.
        /// </summary>
        /// <param name="inputNeurons">The input neurons.</param>
        public Neuron(IEnumerable <Neuron> inputNeurons) : this()
        {
            Ensure.That(inputNeurons).IsNotNull();

            foreach (var inputNeuron in inputNeurons)
            {
                var synapse = new Synapse(inputNeuron, this);
                inputNeuron?.OutputSynapses?.Add(synapse);
                InputSynapses?.Add(synapse);
            }
        }
Example #12
0
 public Neuron(
     IEnumerable <Neuron> inputNeurons,
     Func <double, double> activationFunction,
     Func <double, double> dActivationFunction
     ) : this(activationFunction, dActivationFunction)
 {
     foreach (var inputNeuron in inputNeurons)
     {
         InputSynapses.Add(new Synapse(inputNeuron, this));
         inputNeuron.OutputSynapses.Add(new Synapse(inputNeuron, this));
     }
 }
Example #13
0
        public Neuron(NeuralNetwork NN, IEnumerable <Neuron> inputNeurons)
            : this()
        {
            Network = NN;

            foreach (var inputNeuron in inputNeurons)
            {
                var synapse = new Synapse(inputNeuron, this);
                inputNeuron.OutputSynapses.Add(synapse);
                InputSynapses.Add(synapse);
            }
        }
Example #14
0
    public float CalculateValue()
    {
        /*for (int i = 0; i < InputSynapses.Count; i++)
         * {
         *  xSum += InputSynapses[i].Weight * InputSynapses[i].InputNeuron.Value;
         * }
         ****** é o mesmo que fazer o Sum do LINQ
         */

        float xSum = InputSynapses.Sum(x => x.Weight * x.InputNeuron.Value);
        float u    = xSum + Bias;

        Value = Sigmoid.Output(u);

        return(Value);
    }
Example #15
0
File: Neuron.cs Project: dm-j/nn
 public void UpdateWeights() =>
 InputSynapses.Do(synapse => synapse.Update());
Example #16
0
File: Neuron.cs Project: dm-j/nn
 public virtual void CalculateValue() =>
 Value = Activation(InputSynapses.Sum(synapse => synapse.Weight * synapse.InputNeuron.Value));
Example #17
0
        //public void UpdateWeights(double learnRate, double momentum)
        //{
        //    var prevDelta = BiasDelta;
        //    BiasDelta = learnRate * Gradient;
        //    Bias += BiasDelta + momentum * prevDelta;

        //    foreach (var synapse in InputSynapses)
        //    {
        //        prevDelta = synapse.WeightDelta;
        //        synapse.WeightDelta = learnRate * Gradient * synapse.InputNeuron.Value;
        //        synapse.Weight += synapse.WeightDelta + momentum * prevDelta;
        //    }
        //}
        #endregion

        public void ResetNeuron()
        {
            Initialization();
            InputSynapses.ForEach(synapse => synapse.Initialization());
        }
        public virtual double CalculateValue()
        {
            var d = InputSynapses.Sum(a => a.Weight * a.InputNeuron.Value);    // + Bias;

            return(Value = IsHidden ? NeuralNetwork.SigmoidFunction(d) : NeuralNetwork.IdentityFunction(d));
        }
Example #19
0
 public void AddInput(Synapse input)
 {
     InputSynapses.Add(input);
 }
Example #20
0
 //计算输出值 ,输入值加上偏置,然后应用sigmoid函数
 public virtual double CalculateValue()
 {
     this.InputValue = InputSynapses.Sum(a => a.Weight * a.InputNeuron.OutputValue);
     return(OutputValue = Sigmoid.Output(InputValue + Bias));
 }
Example #21
0
 public virtual double CalculateHiddenValue()
 {
     return(Value = Sigmoid.Relu(InputSynapses.Sum(a => a.Weight * a.InputNeuron.Value)));
 }
 public float CalculateValue()
 {
     return(Value = Sigmoid.Output(InputSynapses.Sum(x => x.Weight * x.InputNeuron.Value) + Bias));
 }
Example #23
0
 public void CalculateValueRecurrent()
 {
     OutputValue = Sigmoid.Output(InputSynapses.Sum(syn => syn.Weight * syn.InputNeuron.LastValue) + Bias);
 }
 /// <summary>
 /// Calculate the value of the neuron based on the weights and values of the Previous Layer
 /// VALUE = SIGMOIDSQUISH(WEIGHT * VALUE) of every previous neuron + bias
 /// </summary>
 /// <returns></returns>
 public float CalculateValue()
 {
     return(Value = NeuralMath.SigmoidSquish(InputSynapses.Sum(s => s.Weight * s.InputNeuron.Value) + Bias));
 }
Example #25
0
        public virtual double CalculateValue()
        {
            var inputSignals = InputSynapses.Sum(a => a.Weight * a.InputNeuron.Value);

            return(Value = ActivationFunction.Output(inputSignals + Bias));
        }
Example #26
0
 public virtual double CalculateValue()                                                             //This function is used in forward propogation process.
 {
     return(Value = Sigmoid.Output(InputSynapses.Sum(a => a.Weight * a.InputNeuron.Value) + Bias)); //Calculate by ably activation function to (weight*input)+bias
 }
Example #27
0
        public void Mutate()
        {
#if false
            int ran = NeuralNet.RandomGenerator.Next(1, 8);
            if (ran == 1)
            {
                return;
            }
            else if (ran > 4)
            {
                InputSynapses.ForEach(syn =>
                {
                    if (NeuralNet.RandomGenerator.Next(1, 2) == 1)
                    {
                        syn.Weight *= 1 + (NeuralNet.RandomGenerator.NextDouble() - 0.5) / 10;
                    }
                });
            }
            else if (ran == 2)
            {
                InputSynapses.ForEach(syn =>
                {
                    if (NeuralNet.RandomGenerator.Next(1, 5) == 1)
                    {
                        syn.Weight *= -1;
                    }
                });
            }
            else if (ran == 3)
            {
                Bias *= 1 + (NeuralNet.RandomGenerator.NextDouble() - 0.5) / 5;
            }
            else if (ran == 4)
            {
                Bias *= -1;
            }
#else
            InputSynapses.ForEach(syn =>
            {
                var ran = NeuralNet.RandomGenerator.NextDouble();
                if (ran < Constants.Con.mutate_percent_of_synapses_uniform)
                {
                    syn.Weight += NeuralNet.RandomGenerator.NextGaussian(0, Constants.Con.mutation_power_synapse);
                }
                else if (ran < Constants.Con.mutate_percent_of_synapses_uniform + Constants.Con.mutate_percent_of_synapses_new_values)
                {
                    syn.Weight = NeuralNet.RandomGenerator.NextGaussian(0, Constants.Con.init_stdDev_synapse);
                }
                syn.Weight = Math.Max(-30, Math.Min(30, syn.Weight));
            });
            {//bias
                var ran = NeuralNet.RandomGenerator.NextDouble();
                if (ran < Constants.Con.mutate_percent_of_biases_uniform)
                {
                    Bias += NeuralNet.RandomGenerator.NextGaussian(0, Constants.Con.mutation_power_bias);
                }
                else if (ran < Constants.Con.mutate_percent_of_synapses_uniform + Constants.Con.mutate_percent_of_biases_new_values)
                {
                    Bias = NeuralNet.RandomGenerator.NextGaussian(0, Constants.Con.init_stdDev_bias);
                }
                Bias = Math.Max(-30, Math.Min(30, Bias));
            }
#endif
        }
Example #28
0
 public virtual double CalculateValue()
 {
     return(Value = Sigmoid.Output(InputSynapses.Sum(a => a.Weight * a.InputNeuron.Value) + Bias));
 }
Example #29
0
 public virtual double CalculateValue()
 {
     return(Value = NeuralNetwork.SigmoidFunction(InputSynapses.Sum(a => a.Weight * a.InputNeuron.Value) + Bias));
 }