W1A2E2 One Hot Vector


I created the one hot vector but it causes dimension issue. What can be done? Thank you for your time.

Hi @Retainer_No.501

You need to check how idx is calculated. How could it have the value of 2354?

1 Like

Thanks for the tips. As I find later, this ravel function tends to make an array 100 times as large:


27 and 2700 are outputs of my commands print(len(y)) and print(len(y.ravel())).

Sorry, but I don’t believe you. Something else is going on. All the ravel() method does is “unroll” or “flatten” an array into a 1D array. I added some instrumentation to my sample function and here’s what I get:

vocab_size = 27
Wax (100, 27) x (27, 1) Waa (100, 100) a_prev (100, 1)
y.shape (27, 1)
len(y) 27
len(y.ravel()) 27

Maybe the real problem is that your y value is the wrong shape. E.g. perhaps it is (27, 100) instead of (27, 1). How could that happen?

One other thing to note: in your earlier post, your initialization of x does not look correct. With that syntax, x will end up being a 1D array. It should be a 2D array. The argument to numpy zeros needs to be “tuple”, right?

The initialization of x should be the reason, as the problem was solved after I wrote np.zeros((n_x,1)) for it. But I still don’t understand why that can cause a *100 effect. Is this explainable?

Did you try printing the shape of your y value in the case that you were seeing the “100 effect”? Everything in programming is explainable: things don’t just happen randomly or by magic. If you can’t explain it yet, that just means you’re not thinking hard enough. :nerd_face:

One thing that may be part of the answer is that you are probably making an incorrect assumption about what the len() function does in python when you apply it to a numpy array. It does not give you the total number of elements in the array: it gives you the number of indices in the first dimension of the array. Watch this:

y = np.random.randn(27, 100)
print(y.shape)
print(len(y))
(27, 100)
27

If you want the number of elements in an array, here’s one way to get that:

print(y.size)
2700
2 Likes