值错误:形状(1,)和(2,1)未对齐

时间:2018-06-15 15:01:12

标签: python numpy

首先我导入了numpy库:

import numpy as np

完成后,我创建了NeuralNetwork类:

class NeuralNetwork(object):
    def __init__(self, input_nodes, hidden_nodes, output_nodes, learning_rate):
        self.input_nodes = input_nodes
        self.hidden_nodes = hidden_nodes
        self.output_nodes = output_nodes

        self.weights_input_to_hidden  = np.random.normal(0.0, self.input_nodes ** -0.5,
                                                         (self.input_nodes,
                                                          self.hidden_nodes))
        self.weights_hidden_to_output = np.random.normal(0.0, self.hidden_nodes ** -0.5,
                                                         (self.hidden_nodes,
                                                          self.output_nodes))

        self.lr = learning_rate

此方法执行sigmoid操作:

def sigmoid(x):
    return 1/(1+np.exp(-x))
    self.activation_function=sigmoid

def train(self, features, targets):
    n_records = features.shape[0]
    delta_weights_i_h = np.zeros(self.weights_input_to_hidden.shape)
    delta_weights_h_o = np.zeros(self.weights_hidden_to_output.shape)

    for X, y in zip(features, targets):
        final_outputs, hidden_outputs=self.forward_pass_train(X)
        delta_weights_i_h, delta_weights_h_o = self.backpropagation(final_outputs,
                                                                    hidden_outputs,
                                                                    X, y,
                                                                    delta_weights_i_h,
                                                                    delta_weights_h_o)

    self.update_weights(delta_weights_i_h, delta_weights_h_o, n_records)


def forward_pass_train(self, X):
    hidden_inputs=np.dot(X, self.weights_input_to_hidden)
    hidden_outputs=self.activation_function(hidden_inputs)

    final_inputs=np.dot(hidden_outputs, self.weights_hidden_to_output)
    final_outputs=self.activation_function(final_inputs)
    return final_outputs, hidden_outputs

此方法实现了反向传播步骤,即发生错误的步骤。

def backpropagation(self, final_outputs, hidden_outputs, X, y, delta_weights_i_h, delta_weights_h_o):
    '''final_outputs: Output from forward pass
        y: target label
        delta_weights_i_h: Change in weights from input to hidden
        delta_weights_h_o: Change in weights from hidden to output
    '''
    error=y-final_outputs
    output_error_term=error*final_outputs*(1-final_outputs)
    hidden_error=np.dot(output_error_term, self.weights_hidden_to_output)    
    hidden_error_term=hidden_error*hidden_outputs*(1-hidden_outputs)

    delta_weights_i_h+=output_error_term*hidden_outputs
    delta_weights_h_o+=hidden_error_term[:,None]*hidden_outputs[:,None]
    return delta_weights_i_h, delta_weights_h_o    

def update_weights(self, delta_weights_i_h, delta_weights_h_o, n_records):
    '''delta_weights_i_h: Change in weights from input to hidden layers
        delta_weights_h_o: Change in weights from hidden to output layers
        n_records: Number of records
    '''
    self.weights_hidden_to_output+=self.lr*delta_weights_h_o/n_records
    self.weights_input_to_hidden+=self.lr*delta_weights_i_h/n_records

def run(self, features):
    hidden_inputs=self.forward_pass_train(features)
    hidden_outputs=self.activation_function(hidden_inputs)

    final_inputs=self.forward_pass_train(hidden_outputs)
    final_outputs=self.activation_function(final_inputs)
    return final_outputs

iterations=100
learning_rate=0.1
hidden_nodes=2
output_nodes=1

我得到的错误看起来像这样:

ERROR: test_train (__main__.TestMethods)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "<ipython-input-12-b897b3b5446c>", line 18, in test_train
    network.train(inputs, targets)
  File "C:/Users/shivaraj/Desktop/Deep Learning/Module_1_Face_Recognition/face_recognition_nocomment.py", line 28, in train
    delta_weights_i_h, delta_weights_h_o=self.backpropagation(final_outputs, hidden_outputs, X,y, delta_weights_i_h, delta_weights_h_o)
  File "C:/Users/shivaraj/Desktop/Deep Learning/Module_1_Face_Recognition/face_recognition_nocomment.py", line 51, in backpropagation
    hidden_error=np.dot(output_error_term, self.weights_hidden_to_output)
ValueError: shapes (1,) and (2,1) not aligned: 1 (dim 0) != 2 (dim 0)

0 个答案:

没有答案