在python cod中获取值时出错

2024-09-29 00:16:50 发布

您现在位置:Python中文网/ 问答频道 /正文

我在函数nm\u模型中得到了一个值错误,但是在前向传播函数中的值对于A2是正确的,我不明白为什么会得到这个错误。你知道吗

import numpy as np
for i in range(0, num_iterations):

    # Forward propagation. Inputs: "X, parameters". Outputs: "A2, cache".
    A2, cache = forward_propagation(X, parameters)

我这里有个错误 这是我的错误

ValueError                                Traceback (most recent call last)
<ipython-input-132-7a346e8aefff> in <module>()
      1 X_assess, Y_assess = nn_model_test_case()
----> 2 parameters = nn_model(X_assess, Y_assess, 4, num_iterations=10000, print_cost=True)
      3 print("W1 = " + str(parameters["W1"]))
      4 print("b1 = " + str(parameters["b1"]))
      5 print("W2 = " + str(parameters["W2"]))

<ipython-input-131-e266bdc33aa9> in nn_model(X, Y, n_h, num_iterations, print_cost)
     33         ### START CODE HERE ### (≈ 4 lines of code)
     34         # Forward propagation. Inputs: "X, parameters". Outputs: "A2, cache".
---> 35         A2, cache = forward_propagation(X, parameters)
     36 
     37         # Cost function. Inputs: "A2, Y, parameters". Outputs: "cost".

<ipython-input-129-32057a9db96b> in forward_propagation(X, parameters)
     21     # Implement Forward Propagation to calculate A2 (probabilities)
     22     ### START CODE HERE ### (≈ 4 lines of code)
---> 23     Z1 = np.dot(W1, X) + b1
     24     A1 = np.tanh(Z1)
     25     Z2 = np.dot(W2, A1) + b2

ValueError: shapes (4,6) and (2,3) not aligned: 6 (dim 1) != 2 (dim 0)

Tags: ina2cache错误npoutputsnumforward
2条回答

初始化X时出错。我在尝试X.shape[0]时使用了X.size进行初始化,效果很好。你知道吗

在正推法中,取数组W1和X的点积,这与使用二维数组时的矩阵乘法相一致。这些数组的尺寸对于点积运算是不正确的,因此存在错误。你知道吗

相关问题 更多 >