I have an Assertion error in nn_model() week 3 Assignment


AssertionError Traceback (most recent call last)
in
1 t_X, t_Y = nn_model_test_case()
----> 2 parameters = nn_model(t_X, t_Y, 4, num_iterations=10000, print_cost=True)
3
4 print("W1 = " + str(parameters[“W1”]))
5 print("b1 = " + str(parameters[“b1”]))

in nn_model(X, Y, n_h, num_iterations, print_cost)
45 # YOUR CODE STARTS HERE
46
—> 47 A2, cache = forward_propagation(X, parameters)
48
49 cost = compute_cost(A2, Y, parameters)

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

AssertionError:

It has something to do with forward propagation but i cant get it .Please someone help

Dear TalhaChughtai;

Assert in python is a function that verifies a statement. If the statement is correct you don’t see anything, if wrong it returns an AssertionError.

In this case means the shape of the array A2 is different than the condition, in your case the size of A2 is different than (1 row, number of columns of array X)

I have just joined the course but from my experience in python, and since you try to assert the dimension of an array that only has 1 row it might be thatA2 is a 1-dimension array and not a 2-dimension one, that is to say, A2 is equal to only X.shape[1], that is (A2.shape == X.shape[1])

Maybe try just check A2.shape and check the dimension of the array, if we consider a 24 columns array
1d = (24,)
2d = (1, 24)

hope it help

1 Like

Checked it already but nothing happen.That statements are not written by me this was the part of assignment which is written already

I think you’re missing the point: the reason that assertion failed is that your A2 value is the wrong shape. That A2 value was produced by your code. Now you need to understand why it is the wrong shape. The first step is what Lucas suggested: you need to print the shape to understand what it is. That may give you some clue as to what went wrong. E.g. put a print statement like this in your code after you compute A2:

print(f"A2.shape = {A2.shape}")

So why is it the wrong shape?

Also note that the bug may not be in forward_propagation: it may be higher up the call stack meaning that you passed incorrect parameters down to forward_propagation. But the debugging process starts from the point of the error and then you work backwards to figure out what went wrong.