Week 2 A2 Exercise 4 - initialize_with_zeros

may i ask why TypeError: data type not understood from my run?
the function has been defined …

[Moderator edit: code removed]

Hi @Lucy_Hui,
{mentor edit to remove the code}
The dimensions you pass to np.zeros() should be a tuple.

That is not the correct way to invoke the function np.zeros. You can find the documentation by googling “numpy zeros”.

The meaning of that message is that the “zeros” function is telling you that it does not like the arguments that you passed to it.

I have defined w = np.zeros((3,1)) (although I don’t understand the reason for the two parentheses)) and b = 0.0 and got a “First test passed!”, but with still “AssertionError: Wrong shape for w. (3, 1) != (4, 1)”.
Did I pass? Or should I correct this difference in the expected shape?

Thanks in advance!

There are two important points there:

  1. The reason for the “nested” parentheses there is that the np.zeros function takes a “tuple” as the argument giving the shape of the desired output. You can learn more by googling “numpy zeros” and reading the documentation.
  2. The second point is if you hard code the dimensions as (3,1), then the function is useless except in the case that the w vector has 3 elements, right? If you were only handling that one case then what’s the point of making a function out of it. The point of a function is that it should be reusable in all cases, even if the requested number of features is 4 or 42 or 12288.

So how can you fix the “hard-coding” problem? You are passed the requested dimension as an argument, right?

1 Like

Got it!
I replaced the hard-coding by “dim, 1”, and it worked!
But for b (expected to be an “initialized scalar”), there is no argument passed. So I “hardcoded” as 0.0. Is that right?

And thank you for your quick response!

Yes, the contents of both w and b are zeros (in floating point). But in the case of b, we know it is a scalar, so there is no choice you need to make there.

1 Like