def train(self, inputs, targets): # Generating the hidden outputs inputs = Matrix.FromArray(inputs) hidden = Matrix.Multiply(self.weights_ih, inputs) hidden.addElementWise(self.bias_h) # Activation here hidden.map(sigmoid) # Generating the output outputs outputs = Matrix.Multiply(self.weights_ho, hidden) outputs.addElementWise(self.bias_o) outputs.map(sigmoid) # Converting array to new Matrix objects targets = Matrix.FromArray(targets) # Calculating the errors output_errors = Matrix.Subtract(targets, outputs) # Calculate gradient gradients = Matrix.Map(outputs, derivsigmoid) gradients.multiplyElementWise(output_errors) gradients.multiply(self.learningrate) # Calculate deltas hidden_t = Matrix.Transpose(hidden) weights_ho_deltas = Matrix.Multiply(gradients, hidden_t) # Adjust Hidden -> Output weights self.weights_ho.addElementWise(weights_ho_deltas) # Adjust Hidden -> Output bias self.bias_o.addElementWise(gradients) # Transposing the hidden-output weights and calculating those errors weights_ho_t = Matrix.Transpose(self.weights_ho) hidden_errors = Matrix.Multiply(weights_ho_t, output_errors) # Caluclate hidden gradient hidden_gradient = Matrix.Map(hidden, derivsigmoid) hidden_gradient.multiplyElementWise(hidden_errors) hidden_gradient.multiply(self.learningrate) # Calculate hidden deltas inputs_t = Matrix.Transpose(inputs) weights_ih_deltas = Matrix.Multiply(hidden_gradient, inputs_t) # Adjust Input -> Hidden weights self.weights_ih.addElementWise(weights_ih_deltas) # Adjust Input -> Hidden bias self.bias_h.addElementWise(hidden_gradient) return output_errors
def feedforward(self, input_array): # Generating the hidden outputs input = Matrix.FromArray(input_array) hidden = Matrix.Multiply(self.weights_ih, input) hidden.addElementWise(self.bias_h) # Activation here hidden.map(sigmoid) # Generating the output outputs output = Matrix.Multiply(self.weights_ho, hidden) output.addElementWise(self.bias_o) output.map(sigmoid) return output.toArray()