我在尝试运行“火车”时遇到了错误值。功能如下。如何使用点积解决问题?
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)
答案 0 :(得分:1)
检查输入和weights_input_to_hidden的形状。我相信你可能不需要Transpose。
答案 1 :(得分:0)
所以你只需要改变 hidden_inputs = np.dot(inputs,self.weights_input_to_hidden.T) 至 hidden_inputs = np.dot(self.weights_input_to_hidden,输入) ?