# AssertionError in second test in 4.2

**URL:** <https://community.deeplearning.ai/t/assertionerror-in-second-test-in-4-2/792231>\
**Category:** Neural Networks and Deep Learning\
**Tags:** week-module-2, coursera-platform\
**Created:** [March 27, 2025, 8:42pm UTC](https://community.deeplearning.ai/t/assertionerror-in-second-test-in-4-2/792231 "2025-03-27T20:42:36Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![jppetraud](https://avatars.discourse-cdn.com/v4/letter/j/858c86/32.png) [@jppetraud](https://community.deeplearning.ai/u/jppetraud)\
**Post date:** [March 27, 2025, 8:42pm UTC](https://community.deeplearning.ai/t/assertionerror-in-second-test-in-4-2/792231/1 "2025-03-27T20:42:37Z")

</div>

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)

---

<div class="post-metadata">

**Author:** ![jppetraud](https://avatars.discourse-cdn.com/v4/letter/j/858c86/32.png) [@jppetraud](https://community.deeplearning.ai/u/jppetraud)\
**Post date:** [March 27, 2025, 8:54pm UTC](https://community.deeplearning.ai/t/assertionerror-in-second-test-in-4-2/792231/2 "2025-03-27T20:54:10Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![paulinpaloalto](https://sea1.discourse-cdn.com/dlai/user_avatar/community.deeplearning.ai/paulinpaloalto/32/187_2.png) [@paulinpaloalto](https://community.deeplearning.ai/u/paulinpaloalto)\
**Post date:** [March 27, 2025, 9:13pm UTC](https://community.deeplearning.ai/t/assertionerror-in-second-test-in-4-2/792231/3 "2025-03-27T21:13:18Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![jppetraud](https://avatars.discourse-cdn.com/v4/letter/j/858c86/32.png) [@jppetraud](https://community.deeplearning.ai/u/jppetraud)\
**Post date:** [March 27, 2025, 9:39pm UTC](https://community.deeplearning.ai/t/assertionerror-in-second-test-in-4-2/792231/4 "2025-03-27T21:39:52Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![paulinpaloalto](https://sea1.discourse-cdn.com/dlai/user_avatar/community.deeplearning.ai/paulinpaloalto/32/187_2.png) [@paulinpaloalto](https://community.deeplearning.ai/u/paulinpaloalto)\
**Post date:** [March 27, 2025, 10:19pm UTC](https://community.deeplearning.ai/t/assertionerror-in-second-test-in-4-2/792231/5 "2025-03-27T22:19:20Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![jppetraud](https://avatars.discourse-cdn.com/v4/letter/j/858c86/32.png) [@jppetraud](https://community.deeplearning.ai/u/jppetraud)\
**Post date:** [March 27, 2025, 10:51pm UTC](https://community.deeplearning.ai/t/assertionerror-in-second-test-in-4-2/792231/6 "2025-03-27T22:51:05Z")

</div>

Super helpful! Thanks!

---

<div class="post-metadata">

**Author:** ![jppetraud](https://avatars.discourse-cdn.com/v4/letter/j/858c86/32.png) [@jppetraud](https://community.deeplearning.ai/u/jppetraud)\
**Post date:** [March 27, 2025, 11:00pm UTC](https://community.deeplearning.ai/t/assertionerror-in-second-test-in-4-2/792231/7 "2025-03-27T23:00:59Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![paulinpaloalto](https://sea1.discourse-cdn.com/dlai/user_avatar/community.deeplearning.ai/paulinpaloalto/32/187_2.png) [@paulinpaloalto](https://community.deeplearning.ai/u/paulinpaloalto)\
**Post date:** [March 27, 2025, 11:04pm UTC](https://community.deeplearning.ai/t/assertionerror-in-second-test-in-4-2/792231/8 "2025-03-27T23:04:32Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![paulinpaloalto](https://sea1.discourse-cdn.com/dlai/user_avatar/community.deeplearning.ai/paulinpaloalto/32/187_2.png) [@paulinpaloalto](https://community.deeplearning.ai/u/paulinpaloalto)\
**Post date:** [March 27, 2025, 11:05pm UTC](https://community.deeplearning.ai/t/assertionerror-in-second-test-in-4-2/792231/9 "2025-03-27T23:05:14Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jppetraud](https://avatars.discourse-cdn.com/v4/letter/j/858c86/32.png) [@jppetraud](https://community.deeplearning.ai/u/jppetraud)\
**Post date:** [March 27, 2025, 11:19pm UTC](https://community.deeplearning.ai/t/assertionerror-in-second-test-in-4-2/792231/10 "2025-03-27T23:19:50Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![paulinpaloalto](https://sea1.discourse-cdn.com/dlai/user_avatar/community.deeplearning.ai/paulinpaloalto/32/187_2.png) [@paulinpaloalto](https://community.deeplearning.ai/u/paulinpaloalto)\
**Post date:** [March 27, 2025, 11:28pm UTC](https://community.deeplearning.ai/t/assertionerror-in-second-test-in-4-2/792231/11 "2025-03-27T23:28:10Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![jppetraud](https://avatars.discourse-cdn.com/v4/letter/j/858c86/32.png) [@jppetraud](https://community.deeplearning.ai/u/jppetraud)\
**Post date:** [March 27, 2025, 11:32pm UTC](https://community.deeplearning.ai/t/assertionerror-in-second-test-in-4-2/792231/12 "2025-03-27T23:32:59Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![paulinpaloalto](https://sea1.discourse-cdn.com/dlai/user_avatar/community.deeplearning.ai/paulinpaloalto/32/187_2.png) [@paulinpaloalto](https://community.deeplearning.ai/u/paulinpaloalto)\
**Post date:** [March 27, 2025, 11:34pm UTC](https://community.deeplearning.ai/t/assertionerror-in-second-test-in-4-2/792231/13 "2025-03-27T23:34:00Z")

</div>

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.
