/// <summary> /// Trains the network with inputs and targets. /// </summary> /// <param name="input_lst"></param> /// <param name="targets_lst"></param> public void Train(List <Double> input_lst, List <Double> targets_lst) { // Turn input and target arrays into matrices Matrix inputs = Matrix.FromList(input_lst); Matrix targets = Matrix.FromList(targets_lst); // The input to the hidden layer is the weights (wih) multiplied by inputs var hidden_inputs = Matrix.MultiplyMatrices(Wih, inputs); // The outputs of the hidden layer pass through sigmoid activation function var hidden_outputs = Matrix.Map(hidden_inputs, Activation); // The input to the output layer is the weights (who) multiplied by hidden layer var output_inputs = Matrix.MultiplyMatrices(Who, hidden_outputs); // The output of the network passes through sigmoid activation function var outputs = Matrix.Map(output_inputs, Activation); // Error is TARGET - OUTPUT var output_errors = Matrix.SubtractMatrices(targets, outputs); // ~ Back prop. // Transpose hidden <-> output weights var whoT = Who.Transpose(); // Hidden errors is output error multiplied by weights (who) var hidden_errors = Matrix.MultiplyMatrices(whoT, output_errors); // Calculate the gradient, this is much nicer in python! var gradient_output = Matrix.Map(outputs, Derivative); // Weight by errors and learing rate gradient_output.Multiply(output_errors); gradient_output.Multiply(LearningRate); // Gradients for next layer, more back propogation! var gradient_hidden = Matrix.Map(hidden_outputs, Derivative); // Weight by errors and learning rate gradient_hidden.Multiply(hidden_errors); gradient_hidden.Multiply(LearningRate); // Change in weights from HIDDEN --> OUTPUT var trans_hidden_outputs = hidden_outputs.Transpose(); var deltaw_output = Matrix.MultiplyMatrices(gradient_output, trans_hidden_outputs); Who.Add(deltaw_output); // Change in weights from INPUT --> HIDDEN var trans_inputs = inputs.Transpose(); var deltaw_hidden = Matrix.MultiplyMatrices(gradient_hidden, trans_inputs); Wih.Add(deltaw_hidden); }