Esempio n. 1
0
        public void Learn(NeuralLayer fromLayer)
        {
            for (int i = 0; i < Neurons.Count; i++)
            {
                var valueDerivatives = Activate.SoftMaxDer(Neurons[i].OutputDendrites.Select(d => d.Signal.Value).ToArray());
                for (int d = 0; d < Neurons[i].OutputDendrites.Count; d++)
                {
                    var currentDendrite = Neurons[i].OutputDendrites[d];
                    foreach (var fromNeuron in fromLayer.Neurons)
                    {
                        foreach (var fromDendrite in fromNeuron.InputDendrites)
                        {
                            currentDendrite.Gamma += fromDendrite.Gamma * currentDendrite.Weight;
                        }
                    }

                    currentDendrite.Gamma *= ActivationType == ActivationType.SoftMax
                        ? valueDerivatives[d]
                        : Activate.GetActivationDer(ActivationType, currentDendrite.Signal.Value);

                    currentDendrite.UpdateWeightAndBias();
                }

                //foreach (var fromNeuron in fromLayer.Neurons)
                //{
                //    Neurons[i].Gamma += fromNeuron.Gamma * Neurons[i].Weight;
                //}

                //var averageDendriteOutput = Neurons[i].OutputDendrites.Average(d => d.Signal.Value);
                //Neurons[i].Gamma *= Activate.GetActivationDer(ActivationType, averageDendriteOutput);
                //Neurons[i].UpdateWeightAndBias();
            }
        }
        public void AddLayer(
            int neuronCount,
            ActivationType activationType = ActivationType.Tanh)
        {
            var layer = new NeuralLayer(neuronCount, activationType);

            AddLayer(layer);
        }
Esempio n. 3
0
        public void Build()
        {
            int i = 0;

            foreach (var layer in Layers)
            {
                if (i >= Layers.Count - 1)
                {
                    break;
                }
                NeuralLayer nextLayer = Layers[i + 1];
                Create(layer, nextLayer);
                i++;
            }
        }
Esempio n. 4
0
        public void Create(NeuralLayer fromLayer, NeuralLayer toLayer)
        {
            Random rand = new Random();

            foreach (var toNeuron in toLayer.Neurons)
            {
                foreach (var fromNeuron in fromLayer.Neurons)
                {
                    toNeuron.Connections.Add(new Connection()
                    {
                        InputPulse = fromNeuron.OutputPulse, Weight = rand.NextDouble() * 0.5 + 0.001
                    });                                                                                                                          //inicjowanie połączeń losowymi wartościami i przekazywanie pulsu z poprzedzającego neurona
                    toNeuron.Fire();
                }
            }
        }
Esempio n. 5
0
        /// <summary>
        /// Calculates the output of a certain input
        /// </summary>
        /// <param name="input">The numbers for the input layer</param>
        /// <returns>A Matrix that is 1x(Number of output neurons) with the result</returns>
        public Matrix Calculate(float[] input)
        {
            NeuralLayer nextLayer = hiddenLayers.Length > 0 ? hiddenLayers[0] : outputLayer;

            if (input.Length != nextLayer.inputsPerNeuron)
            {
                throw new ArgumentException("There are to " + (input.Length > nextLayer.inputsPerNeuron ? "many" : "few") + " inputs for the next layer to handle");
            }

            Matrix result = (Matrix)input;

            for (int i = 0; i < numHiddenLayers; i++)
            {
                result = hiddenLayers[i].Calculate(result);
            }

            return(outputLayer.Calculate(result));
        }
Esempio n. 6
0
        public void AddLayer(NeuralLayer newLayer)
        {
            int connectionCount = 1;

            if (Layers.Count > 0)
            {
                connectionCount = Layers.Last <NeuralLayer>().Neurons.Count;
            }

            /*
             * foreach (var neuron in newLayer.Neurons)
             * {
             *  for (int i = 0; i < connectionCount; i++)
             *  {
             *     neuron.Connections.Add(new Connection());
             *  }
             * }
             */
            Layers.Add(newLayer);
        }
Esempio n. 7
0
        static void Main(string[] args)
        {
            NeuralNetwork network = new NeuralNetwork();

            NeuralLayer inputLayer = new NeuralLayer(4, 0);

            inputLayer.Neurons[0].OutputPulse = 0.5;
            inputLayer.Neurons[1].OutputPulse = 0.2;
            inputLayer.Neurons[2].OutputPulse = 0.3;
            inputLayer.Neurons[3].OutputPulse = 0.07;
            NeuralLayer hiddenLayer = new NeuralLayer(5, 1);
            NeuralLayer outputLayer = new NeuralLayer(3, 2);

            network.AddLayer(inputLayer);
            network.AddLayer(hiddenLayer);
            network.AddLayer(outputLayer);

            network.Build();
            network.Print();

            Console.Read();
        }
Esempio n. 8
0
 internal void SetLayerOut(NeuralLayer layerOut)
 {
     this.LayerOut         = layerOut;
     this.LayerOut.LayerIn = this;
     this.NeuralLink       = new NeuralLink(this.mathManager, this, this.LayerOut);
 }
 public NeuralLink(MathOperationManager mathManager, float[] matrixData, NeuralLayer layerIn, NeuralLayer layerOut)
 {
     this.Weights = mathManager.CreateMatrix(matrixData, layerOut.Data.Row, layerIn.Data.Row);
     this.ErrorGradient = mathManager.CreateMatrix(layerOut.Data.Row, layerIn.Data.Row);
 }
 public NeuralLink(MathOperationManager mathManager, NeuralLayer layerIn, NeuralLayer layerOut)
 {
     this.Weights = mathManager.CreateMatrix(ArrayUtilities.GetArrayWithRandomValues(layerOut.Data.Row * layerIn.Data.Row), layerOut.Data.Row, layerIn.Data.Row);
     this.ErrorGradient = mathManager.CreateMatrix(layerOut.Data.Row, layerIn.Data.Row);
 }
 public NeuralLink(MathOperationManager mathManager, float[] matrixData, NeuralLayer layerIn, NeuralLayer layerOut)
 {
     this.Weights       = mathManager.CreateMatrix(matrixData, layerOut.Data.Row, layerIn.Data.Row);
     this.ErrorGradient = mathManager.CreateMatrix(layerOut.Data.Row, layerIn.Data.Row);
 }
 public NeuralLink(MathOperationManager mathManager, NeuralLayer layerIn, NeuralLayer layerOut)
 {
     this.Weights       = mathManager.CreateMatrix(ArrayUtilities.GetArrayWithRandomValues(layerOut.Data.Row * layerIn.Data.Row), layerOut.Data.Row, layerIn.Data.Row);
     this.ErrorGradient = mathManager.CreateMatrix(layerOut.Data.Row, layerIn.Data.Row);
 }