DL W2 assignment Assertion error

Hello,
I am at the step of defining b, and after I got this error I added the data type for b: dtype=np.float64, however I keep getting this Assertion error:


AssertionError Traceback (most recent call last)
in
2 w, b = initialize_with_zeros(dim)
3
----> 4 assert type(b) == float
5 print ("w = " + str(w))
6 print ("b = " + str(b))

AssertionError:

In python 0. is not the same thing as 0 is The first is a float and the second is an integer. That’s the simple way to solve this and will give you the correct type. np.float64 is not the same as float.

Thank you Paul, I changed it to: b = np.zeros((1,1), dtype=np.float), however still returns the same error:
AssertionError Traceback (most recent call last)
in
2 w, b = initialize_with_zeros(dim)
3
----> 4 assert type(b) == float
5 print ("w = " + str(w))
6 print ("b = " + str(b))

AssertionError:

@anastasiya27 so I tried to poke around a little to see what it is you are looking at.

And Paul is very correct, and it is not just with zeros, or x = 1 (!=) x = 1. and this can lead to sometimes confusing problems (the first produces an int the second a float).

But, in your case… Honestly I’m not sure using the numpy float type is neccessary-- you should be able to just say ‘float’.

But I think your bigger issue here, which I will leave you to think about, is b a scalar (aka ‘one number’)-- Or a vector ?

Think about that and it should fix your problem.

The problem is that the type is now np.array. You can check that by doing this:

print(f"type(b) = {type(b)}")

The point is that b is a scalar.

You’re working too hard here. I’m not supposed to just write the code for you, but if you say:

x = 1

Then x is a scalar of type int. But if you say:

x = 1.

then x is a python scalar of type float.

1 Like

I didn’t do a great job at explaining this to them Paul, but the point is their rendition of b does not include the fact that b has a dimensionality component to it. It is not (1, 1).

Thank you Paul and Anthony, I was able to fix it

2 Likes

I continue working on the same assignment, at the step of propagate function , I received this error, not sure what it refers to as ‘out’, it doesn’t really gives much information, so I am stuck, tried googling but couldn’t find the solution.

44 # YOUR CODE STARTS HERE
45 db = (I deleted my code here)
—> 46 dw = (I deleted my code here)
47
48 # YOUR CODE ENDS HERE

<array_function internals> in dot(*args, **kwargs)

TypeError: ‘out’ must be an array

I just took a look at the documentation for numpy dot again to refresh my memory. There should be no need to use the “out=” keyword argument when you invoke np.dot for any purpose here in this exercise. Prof Ng gave us examples of how to use np.dot in the lectures. You just write something like:

result = np.dot(a, b)

I had never seen the error message you show, so I did some experimentation. The point of the out keyword in np.dot is that there may be circumstances in which you can get better performance if you preallocated the output array to hold the result. E.g. if you were doing a loop and the target is large and it works logically to reuse it.

But if you are doing that, then out must be preallocated as the exact type and (if it is an array) the correct shape of the output of the dot product. I can get the error message you show by setting out = junk where junk is a precreated variable that is a scalar, as in:

junk = 42.

Then I get exactly the error you show. As I mentioned in my previous reply, there is no need to use out here, but if you do, it needs to be a variable that is preallocated as an array of the correct shape.

Thank you Paul, what you were saying that my out could be of a wrong shape guided me in the right direction. I checked my code again, I wasn’t using out keyword, it turns out my dw formula had parentheses in wrong places and the product was not an array, and I fixed it :slight_smile:

1 Like

Interesting. Yes, I was puzzled about where the “out” came from in the error message. Sorry that my analysis missed the mark, but I’m very glad to hear that you were able to find the solution anyway.