예제 #1
0
    def guess(self, input_array):
        inputs = Matrix.fromArray(input_array)
        hidden = Matrix.MatrixMultiplication(self.weights_ih, inputs)
        hidden.add(self.bias_ih)
        Matrix.mapMatrix(hidden, NeuralNetwork.sigmoid)

        outputs = Matrix.MatrixMultiplication(self.weights_ho, hidden)
        outputs.add(self.bias_ho)
        Matrix.mapMatrix(outputs, NeuralNetwork.sigmoid)

        return outputs
예제 #2
0
    def FeedForward(self, input_array):
        self.inputs = Matrix.fromArray(input_array)
        self.hidden = Matrix.MatrixMultiplication(self.weights_ih, self.inputs)
        self.hidden.add(self.bias_ih)
        Matrix.mapMatrix(self.hidden, NeuralNetwork.sigmoid)

        self.outputs = Matrix.MatrixMultiplication(self.weights_ho,
                                                   self.hidden)
        self.outputs.add(self.bias_ho)
        Matrix.mapMatrix(self.outputs, NeuralNetwork.sigmoid)

        return self.outputs
예제 #3
0
    def train(self, input_array, targets):
        #error = targets - outputs
        inputs = Matrix.fromArray(input_array)
        hidden = Matrix.MatrixMultiplication(self.weights_ih, inputs)
        hidden.add(self.bias_ih)
        Matrix.mapMatrix(hidden, NeuralNetwork.sigmoid)

        outputs = Matrix.MatrixMultiplication(self.weights_ho, hidden)
        outputs.add(self.bias_ho)
        Matrix.mapMatrix(outputs, NeuralNetwork.sigmoid)

        output_errors = copy.deepcopy(outputs)
        output_errors.multiply(-1)
        targets = Matrix.fromArray(targets)
        output_errors.add(targets)
        #print(self.outputs.toArray())
        #print(output_errors.toArray())
        hidden_weights = Matrix.transpose(self.weights_ho)
        hidden_errors = Matrix.MatrixMultiplication(hidden_weights,
                                                    output_errors)
        #print(hidden_errors.toArray())

        gradient = copy.deepcopy(outputs)
        Matrix.mapMatrix(gradient, NeuralNetwork.dsigmoid)
        gradient.multiply(output_errors)
        gradient.multiply(self.lr)
        self.bias_ho.add(gradient)
        hidden_T = Matrix.transpose(hidden)
        weight_ho_deltas = Matrix.MatrixMultiplication(gradient, hidden_T)

        self.weights_ho.add(weight_ho_deltas)

        hidden_gradient = copy.deepcopy(hidden)
        Matrix.mapMatrix(hidden_gradient, NeuralNetwork.dsigmoid)
        hidden_gradient.multiply(hidden_errors)
        hidden_gradient.multiply(self.lr)
        self.bias_ih.add(hidden_gradient)
        inputs_T = Matrix.transpose(inputs)
        weight_ih_deltas = Matrix.MatrixMultiplication(hidden_gradient,
                                                       inputs_T)

        self.weights_ih.add(weight_ih_deltas)