numpy 尽管预期的输出与我的输出匹配,但仍存在Assert错误,显示:“W1形状错误“,但W1形状正确

kadbb459  于 2023-02-04  发布在  其他
关注(0)|答案(1)|浏览(165)

这是Jupyter笔记本实现的深度学习模型。初始化数据时,我得到了预期的输出,但发生了Assert错误,显示W1的形状错误,但W1的形状是正确的。
https://www.coursera.org/learn/neural-networks-deep-learning/programming/e6FsA/planar-data-classification-with-one-hidden-layer/lab?path=%2Fnotebooks%2Frelease%2FW3A1%2FPlanar_data_classification_with_one_hidden_layer.ipynb

def initialize_parameters(n_x, n_h, n_y):
    """
    Argument:
    n_x -- the size of the input layer
    n_h -- the size of the hidden layer
    n_y -- the size of the output layer
    
    Returns:
    params -- python dictionary containing your parameters:
                    W1 -- weight matrix of shape (n_h, n_x)
                    b1 -- bias vector of shape (n_h, 1)
                    W2 -- weight matrix of shape (n_y, n_h)
                    b2 -- bias vector of shape (n_y, 1)
    """    
    #(≈ 4 lines of code)
    # W1 = ...
    # b1 = ...
    # W2 = ...
    # b2 = ...
    # YOUR CODE STARTS HERE
    W1=np.random.randn(4,2)*0.01
    b1=np.zeros((4,1))
    W2=np.random.randn(1,4)*0.01
    b2=np.zeros((1,1))
    # YOUR CODE ENDS HERE

    parameters = {"W1": W1,
                  "b1": b1,
                  "W2": W2,
                  "b2": b2}
    
    return parameters



np.random.seed(2)
n_x, n_h, n_y = initialize_parameters_test_case()
parameters = initialize_parameters(n_x, n_h, n_y)

print("W1 = " + str(parameters["W1"]))
print("b1 = " + str(parameters["b1"]))
print("W2 = " + str(parameters["W2"]))
print("b2 = " + str(parameters["b2"]))

initialize_parameters_test(initialize_parameters)

我的输出:

W1 = [[-0.00416758 -0.00056267],
     [-0.02136196  0.01640271],
     [-0.01793436 -0.00841747],
     [ 0.00502881 -0.01245288]]
    
b1 = [[0.],[0.],[0.],[0.]]
W2 = [[-0.01057952, -0.00909008,  0.00551454,  0.02292208]]
b2 = [[0.]]
    
Expected Output=>
W1 = [[-0.00416758 -0.00056267]
     [-0.02136196  0.01640271]
     [-0.01793436 -0.00841747]
     [ 0.00502881 -0.01245288]]
b1 = [[0.] [0.] [0.] [0.]]
W2 = [[-0.01057952 -0.00909008  0.00551454  0.02292208]]
b2 = [[0.]]

错误:

AssertionError Traceback (most recent call last)
<ipython-input-40-0eb4c3a6d62e> in <module>
      8 print("b2 = " + str(parameters["b2"]))
      9 
---> 10 initialize_parameters_test(initialize_parameters)

~/work/release/W3A1/public_tests.py in initialize_parameters_test(target)
     51     assert type(parameters["b2"]) == np.ndarray, f"Wrong type for b2. Expected: {np.ndarray}"
     52 
---> 53     assert parameters["W1"].shape == expected_output["W1"].shape, f"Wrong shape for W1."
     54     assert parameters["b1"].shape == expected_output["b1"].shape, f"Wrong shape for b1."
     55     assert parameters["W2"].shape == expected_output["W2"].shape, f"Wrong shape for W2."

AssertionError: Wrong shape for W1.
ozxc1zmp

ozxc1zmp1#

请记住使用参数n_x, n_h, n_y,而不是硬编码W1b1W2b2的尺寸

相关问题