/// <summary> /// Feed Forward (FF) this NN with a given input Array /// </summary> /// <param name="inputs">NN Inputs</param> /// <returns></returns> public float[] FeedForward(float[] inputs) { // Add inputs to the Neuron Matrix for (int i = 0; i < inputs.Length; i++) { neurons[0][i] = inputs[i]; } // Itterate over all Neurons and compute FF values for (int i = 1; i < layers.Length; i++) { for (int j = 0; j < layers[i]; j++) { // Init float value = bias ? 1.0f : 0.0f; for (int k = 0; k < neurons[i - 1].Length; k++) { // Sum off all Weights connections of this Neuron Weight values in their previous Layer value += weights[i - 1][j][k] * neurons[i - 1][k]; } // Hyperbolic Tangent activation neurons[i][j] = NNMath.ActivateFunction(value); } } // Return output Layer return neurons[neurons.Length - 1]; }
/// <summary> /// Generate Weight Matrix /// </summary> private void InitWeights() { // Weight Init (will be converted to 3D Array) List<float[][]> weightsList = new List<float[][]>(); // Itterate over all Neurons that have a Weight connection for (int i = 1; i < layers.Length; i++) { // Layer Weight List for this current Layer (will be converted to 2D Array) List<float[]> layerWeightsList = new List<float[]>(); int neuronsInPreviousLayer = layers[i - 1]; // Itterate over all Neurons in this current Layer for (int j = 0; j < layers[i]; j++) { // Neurons Weights float[] neuronWeights = new float[neuronsInPreviousLayer]; // Itterate over all Neurons in the previous Layer and set the Weights randomly between 0.5f and -0.5 for (int k = 0; k < neuronsInPreviousLayer; k++) { // Give random Weights to Neuron Weights neuronWeights[k] = NNMath.RandomWeightValue(); } // Add Neuron Weights of this current Layer to Layer Weights layerWeightsList.Add(neuronWeights); } // Add this Layers Weights converted into 2D Array into Weights List weightsList.Add(layerWeightsList.ToArray()); } // Convert to 3D Array weights = weightsList.ToArray(); }