Week 3 assignment bug in A2 dimension

AssertionError Traceback (most recent call last)
in
----> 1 nn_model_test(nn_model)

~/work/release/W3A1/public_tests.py in nn_model_test(target)
261
262 t_X, t_Y = nn_model_test_case()
→ 263 parameters = target(t_X, t_Y, n_h, num_iterations=10000, print_cost=True)
264
265 print("W1 = " + str(parameters[“W1”]))

in nn_model(X, Y, n_h, num_iterations, print_cost)
33 # Forward propagation. Inputs: “X, parameters”. Outputs: “A2, cache”.
34 # A2, cache = …
—> 35 A2, cache=forward_propagation(X,parameters)
36 # Cost function. Inputs: “A2, Y”. Outputs: “cost”.
37 # cost = …

in forward_propagation(X, parameters)
37 # YOUR CODE ENDS HERE
38
—> 39 assert(A2.shape == (1, X.shape[1]))
40
41 cache = {“Z1”: Z1,

I am getting this error, inspite of the shape of A2, being correct. I have gone through the matrix dimensions multiple times. Help me out with this.
lab id: bbotzymycckz

When you are testing nn_model(), the errors could be in any function that is used by the model:

  • initialize_parameters()
  • forward_propagation()
  • backward_propagation()
  • update_parameters()

So first review very carefully that all of these functoins pass every one of the built-in tests in the notebook.

Tip:
The tests use different sizes of dataset - so be sure your code avoids using fixed values for any of the parameters that should be variables.

1 Like

But the point of that error message is that the shape of A2 is not correct. So the next step would be to show us what the shape of your A2 value actually is.

I added this logic in the “print” if statement:

        # Print the cost every 1000 iterations
        if print_cost and i % 1000 == 0:
            print ("Cost after iteration %i: %f" %(i, cost))
            print(f"A2.shape {A2.shape}")

Here’s what I see when I run the test:

Cost after iteration 0: 0.693086
A2.shape (1, 4)
Cost after iteration 1000: 0.000220
A2.shape (1, 4)
Cost after iteration 2000: 0.000108
A2.shape (1, 4)
Cost after iteration 3000: 0.000072
A2.shape (1, 4)
Cost after iteration 4000: 0.000054
A2.shape (1, 4)
Cost after iteration 5000: 0.000043
A2.shape (1, 4)
Cost after iteration 6000: 0.000036
A2.shape (1, 4)
Cost after iteration 7000: 0.000030
A2.shape (1, 4)
Cost after iteration 8000: 0.000027
A2.shape (1, 4)
Cost after iteration 9000: 0.000024
A2.shape (1, 4)
W1 = [[ 0.71392202  1.31281102]
 [-0.76411243 -1.41967065]
 [-0.75040545 -1.38857337]
 [ 0.56495575  1.04857776]]
b1 = [[-0.0073536 ]
 [ 0.01534663]
 [ 0.01262938]
 [ 0.00218135]]
W2 = [[ 2.82545815 -3.3063945  -3.16116615  1.8549574 ]]
b2 = [[0.00393452]]
All tests passed!
1 Like

This is the forward propogation function , where A2 is created:

Moderator Edit: Solution code removed

the test case after forward propogation function is showing all tests passed.

Sharing your code in a public thread is not allowed.
Regarding your error, as you say, you have passed all the above exercises. So, how you are initializing parameters for this exercise (Exercise 8 - nn_model )? You have to use initialize_parameters function.

Yes, I am using initialize_parameters function to initialize

Send me your code of this function in a private message. Click my name and message.

Thank you for sharing your code, @Uddhav37. So, it is very important to understand what are n_x, n_h, and n_y and in what order they should pass to the initialize_parameters .

1 Like