AssertionError in second test in 4.2

For 4.2 in Logistic Regression with a Neural Network mindset

initialize_with_zeros_test_2(initialize_with_zeros)

I passed the first test but get an assertion error for the second test

AssertionError: Wrong shape for w. (3, 1) != (4, 1)

Adding to my inquiry: If w is (12288,1) as per flatten X and the diagram above, why do we initialize with w = np.zeros((3, 1)).

I do not see any instructions or the videos. Where can I find an explanation?

The functions we are writing here are general in the sense that they can work properly with any shape inputs. You must be referencing global variables or hard-coding some of the dimensions.

Just because the actual image data has 12288 features does not mean that all data we use in the test cases will have that same number of features.

Thanks Paulin, I saw that later in the next cells, but still struggling with the initialization. This is what I have in the first cell:

{moderator edit - solution code removed}

And then in the next one:
dim = 2
w, b = initialize_with_zeros(dim)

assert type(b) == float
print ("w = " + str(w))
print ("b = " + str(b))

initialize_with_zeros_test_1(initialize_with_zeros)
initialize_with_zeros_test_2(initialize_with_zeros)

I pass the first test, but for the second test I get:

AssertionError Traceback (most recent call last)
in
7
8 initialize_with_zeros_test_1(initialize_with_zeros)
----> 9 initialize_with_zeros_test_2(initialize_with_zeros)

~/work/release/W2A2/public_tests.py in initialize_with_zeros_test_2(target)
29 assert b == 0., “b must be 0.0”
30 assert type(w) == np.ndarray, f"Wrong type for w. {type(w)} != np.ndarray"
—> 31 assert w.shape == (dim, 1), f"Wrong shape for w. {w.shape} != {(dim, 1)}"
32 assert np.allclose(w, [[0.], [0.], [0.], [0.]]), f"Wrong values for w. {w} != {[[0.], [0.], [0.], [0.]]}"
33 print(‘\033[92mSecond test passed!’)

AssertionError: Wrong shape for w. (3, 1) != (4, 1)

I don’t get how the first test and the second are requiring something different. It seems that I cannot continue with the rest of the exercise unless I get the initialization right.

Any advice please?

JP

You have hard-coded the number of features to 3, which doesn’t work. You already observed that the real data has 12288 features, so how can this code work both for 3 or 4 or 12288 features? Hint: the function has an argument which gives you the required size.

1 Like

Super helpful! Thanks!

2 Likes

Sorry - looks like I am going to need more hand holding. I keep getting an error, no matter the permutations I run, including simply db = Y or db = A:

{moderator edit - solution code removed}

File “”, line 44
db = 1/m * np.sum (A-Y, axis=1)
^
SyntaxError: invalid syntax

The parentheses do not match on the previous line.

Also please note that we aren’t supposed to just post our code in public and say, in effect, “please fix it for me”.

Please start by just showing the error trace you are getting. I would have been able to answer the question just based on that. When we can’t figure it out without looking at the source code, there are private ways to share that.

Regards,
Paul

1 Like

Note that the editor of the notebook is “syntax aware”. Just click on a parenthesis and it will highlight the matching one. Or not :scream_cat:

Thanks. I understand this is public but I could not figure out how to get help privately. I tried many permutations of the parentheses and I have been going around for hours. Plus, when I delete the code completely, I get the same error, but in the code that I did not write: File “”, line 47
cost = np.squeeze(np.array(cost))
^
SyntaxError: invalid syntax

How can I reach out privately?

That’s because you must have a missing close paren on the previous line to the one that throws the error, which is code that you did write.

We can use DMs to share code privately, but there should not be any need yet.

Please check all the parens using the trick that I just described above.

It’s the same lesson we just learned on the previous error, right?

Thanks! It worked. Did not understand that the syntax error was before the arrow. Thank you for your patience!

It’s a good lesson about python. When it hits the end of the line and it’s still looking for a close paren, it goes on to the next line and then it “Throws” there when it sees something other than the close parenthesis that it is looking for.

2 Likes