Exemple #1
0
  def train(self, input_list, target_list):
    """
    Trains the neural network using the given input_list and target_array.

    Args:
      input_list: List of numbers containing the input values.
      target_list: List of numbers containing the target values.

    Returns:
      Returns the resulting neural network.
    """

    inputs = Matrix.from_list(input_list)
    hidden = Matrix.dot_product(self.weights_ih, inputs)
    hidden.add_matrix(self.bias_h)
    hidden.map(self.activation_function.func)

    outputs = Matrix.dot_product(self.weights_ho, hidden)
    outputs.add_matrix(self.bias_o)
    outputs.map(self.activation_function.func)

    targets = Matrix.from_list(target_list)

    # Calculate the error
    output_errors = targets.sub_matrix(outputs, False)

    # Calculate gradient
    gradients = outputs.map(self.activation_function.dfunc, False)
    gradients.mul_matrix(output_errors)
    gradients.mul_scalar(self.learning_rate)

    # Calculate deltas
    hidden_t = Matrix.transpose(hidden)
    weights_ho_deltas = Matrix.dot_product(gradients, hidden_t)

    # Adjust the weights and the biases by the calculated deltas
    self.weights_ho.add_matrix(weights_ho_deltas)
    self.bias_o.add_matrix(gradients)


    # Calculate the hidden layer errors
    weights_ho_t = Matrix.transpose(self.weights_ho)
    hidden_errors = Matrix.dot_product(weights_ho_t, output_errors)

    # Calculate hidden gradient
    hidden_gradients = hidden.map(self.activation_function.dfunc, False)
    hidden_gradients.mul_matrix(hidden_errors)
    hidden_gradients.mul_scalar(self.learning_rate)

    # Calculate input -> hidden deltas
    inputs_t = Matrix.transpose(inputs)
    weights_ih_deltas = Matrix.dot_product(hidden_gradients, inputs_t)

    # Adjust the weights and the biases by the calculated deltas
    self.weights_ih.add_matrix(weights_ih_deltas)
    self.bias_h.add_matrix(hidden_gradients)

    return self
Exemple #2
0
  def predict(self, input_list):
    """
    Calculates the outputs using the given inputs.

    Args:
      input_list: List of numbers containing the input values.

    Returns:
      Returns a list of numbers containing the outputs.
    """

    inputs = Matrix.from_list(input_list)
    hidden = Matrix.dot_product(self.weights_ih, inputs)
    hidden.add_matrix(self.bias_h)
    hidden.map(self.activation_function.func)

    outputs = Matrix.dot_product(self.weights_ho, hidden)
    outputs.add_matrix(self.bias_o)
    outputs.map(self.activation_function.func)

    return outputs.to_list()