2017-02-08 63 views
0

嘗試按如下方式運行'train'函數時出現值錯誤。如何解決點積問題?ValueError:形狀(56,1)和(56,2)未對齊:1(dim 1)!= 56(dim 0)

def train(self, inputs_list, targets_list): 
     # Convert inputs list to 2d array 
     inputs = np.array(inputs_list, ndmin=2).T 
     targets = np.array(targets_list, ndmin=2).T 

     #### Implement the forward pass here #### 
     ### Forward pass ### 
     # TODO: Hidden layer 
     hidden_inputs = np.dot(inputs,self.weights_input_to_hidden.T) 
     # signals into hidden layer 
     hidden_outputs = self.sigmoid(hidden_inputs) 
     # signals from hidden layer 
     print(hidden_outputs) 

     # TODO: Output layer 
     final_inputs = np.dot(hidden_outputs,self.weights_hidden_to_output) 
     # signals into final output layer 
     final_outputs = final_inputs 
     # signals from final output layer 

     #### Implement the backward pass here #### 
     ### Backward pass ### 

     # TODO: Output error 
     output_errors = final_outputs - targets_list 
     # Output layer error is the difference between desired target and actual output. 

     # TODO: Backpropagated error 
     hidden_errors = np.dot(output_errors,self.weights_hidden_to_output) 
     # errors propagated to the hidden layer 
     hidden_grad = hidden_outputs * (1 - hidden_outputs) 
     # hidden layer gradients 

     # TODO: Update the weights 
     self.weights_hidden_to_output += self.lr * output_errors * hidden_outputs 
     # update hidden-to-output weights with gradient descent step 
     self.weights_input_to_hidden += self.lr * hidden_errors * hidden_grad * inputs 
     # update input-to-hidden weights with gradient descent step 


<ipython-input-21-c3bea2c48af8> in train(self, inputs_list, targets_list) 
    31   ### Forward pass ### 
    32   # TODO: Hidden layer 
---> 33   hidden_inputs = np.dot(inputs,self.weights_input_to_hidden.T) 
    34   # signals into hidden layer 
    35   hidden_outputs = self.sigmoid(hidden_inputs) 

ValueError: shapes (56,1) and (56,2) not aligned: 1 (dim 1) != 56 (dim 0) 

回答

1

檢查輸入和weights_input_to_hidden的形狀。我相信你可能不需要轉置。

+0

謝謝,你說得對。打印出形狀後,我設法找出了矩陣乘法。 –

+0

我試圖刪除轉置...仍然顯示錯誤 –

+1

Satyaki,實際上要解決它,你不需要轉置爲投入的形狀是(56,1)和weights_input_to_hidden的形狀是(2,56)。我基本上將矩陣乘法的順序切換到weights_inputs_to_hidden x輸入,而不是它的工作。 –

0

,所以你只需要改變 hidden_​​inputs = np.dot(輸入,self.weights_input_to_hidden.T) 到 hidden_​​inputs = np.dot(self.weights_input_to_hidden,輸入) ?

+0

是的,沒錯,Ney J Torres。 –

相關問題