/// <summary> /// Updates own synapse weights /// </summary> /// <param name="ownDelta">The delta values of this network</param> /// <param name="previousLayerValues">The values of the previous layer</param> /// <param name="learnParameter">The resolution to learn at</param> public void UpdateValues(double[] ownDelta, double[] previousLayerValues, double learnParameter) { //Calculate and add weight deltas for (int i = 0; i < ownDelta.Length; i++) { for (int j = 0; j < previousLayerValues.Length; j++) { _neurons[i][j] += NeuronMath.GetWeightDelta(learnParameter, ownDelta[i], previousLayerValues[j]); } } double biasDelta = 0; //Calculate bias delta for (int i = 0; i < NeuronCount; i++) { biasDelta += ownDelta[i] * _neurons[i][_neurons[i].Length - 1]; } //Update connection weights to bias for (int i = 0; i < NeuronCount; i++) { _neurons[i][_neurons[i].Length - 1] *= NeuronMath.GetWeightDelta(learnParameter, biasDelta, Bias); } }
/// <summary> /// Returns the neuron delta values for an output layer /// </summary> /// <param name="isValues">The values the neurons in this layer have</param> /// <param name="wantValues">The values the neurons in this layer should have</param> /// <returns>The delta values for the neurons</returns> public double[] GetDeltaOutputLayer(double[] isValues, double[] wantValues) { //Create result array var result = new double[NeuronCount]; //Calculate delta values for (int i = 0; i < result.Length; i++) { result[i] = NeuronMath.GetDeltaOutputLayer(Derivative(isValues[i]), isValues[i], wantValues[i]); } return(result); }
/// <summary> /// Returns the neuron delta values for an output layer /// </summary> /// <param name="isValues">The values the neurons in this layer have</param> /// <param name="calcValues">The calc values of the next layer</param> /// <returns>The delta values for the neurons</returns> public double[] GetDeltaHiddenLayer(double[] isValues, double[] calcValues) { //Create result array var result = new double[calcValues.Length]; //Calculate delta values for (int i = 0; i < calcValues.Length; i++) { result[i] = NeuronMath.GetDeltaHiddenLayer(isValues[i], calcValues[i]); } return(result); }
/// <summary> /// Returns the derivative of the activation function in the layer /// </summary> /// <param name="value">The value to calculate</param> /// <returns>The result</returns> private double Derivative(double value) { //Check own type switch (Type) { //Case Sigmoid: case LayerType.Sigmoid: //Return SigmoidDerivative of value return(NeuronMath.SigmoidDerivative(value)); //This should not happen default: throw new Exception("This should not happen !"); } }
/// <summary> /// Claculates the neuron values for a forward pass in this layer /// </summary> /// <param name="input">The values of the previous layer</param> /// <returns>The processed values</returns> public double[] ForwardPass(double[] input) { //Check input size if (input.Count() != _neurons[0].Length - 1) { throw new ArgumentException("Invalid input array size !"); } //Add bias to input Array.Resize(ref input, input.Length + 1); input[input.Length - 1] = Bias; var result = new double[NeuronCount]; //Calculate neuron values for (var i = 0; i < NeuronCount; i++) { result[i] = ActivationFunction(NeuronMath.MatrixMultiply(_neurons[i], input)); } return(result); }