Why am I getting a shape of (2,5) for X?


ValueError Traceback (most recent call last)
in
1 # Note:
2 # Actual values are not checked here in the unit tests (due to random initialization).now3_unittest.test_nn_model(nn_model)
----> 3 w3_unittest.test_nn_model(nn_model)

~/work/w3_unittest.py in test_nn_model(target_nn_model)
357 for test_case in test_cases:
358
→ 359 result = target_nn_model(test_case[“input”][“X”], test_case[“input”][“Y”], test_case[“input”][“num_iterations”], False)
360
361 try:

in nn_model(X, Y, num_iterations, print_cost)
28 ### START CODE HERE ### (~ 2 lines of code)
29 # Forward propagation. Inputs: “X, parameters”. Outputs: “Y_hat”.
—> 30 Y_hat = forward_propagation(X, parameters)
31
32 # Cost function. Inputs: “Y_hat, Y”. Outputs: “cost”.

in forward_propagation(X, parameters)
18 # Implement Forward Propagation to calculate Z.
19 ### START CODE HERE ### (~ 2 lines of code)
—> 20 Z = np.dot(W, X) + b
21 Y_hat = Z
22 ### END CODE HERE ###

<array_function internals> in dot(*args, **kwargs)

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

One of those matrices W or X needs to be transposed so multiplication can happen, read a bit about matrix multiplication.

If W is size (1,1), then it is impossible for the size of X to be (2,5). This is because the number of values in W (the weights) must be equal to the number of features in X.

So in forward_propagation() at line 20, either your W is wrong, or your X is wrong.

1 Like