DLS course5, wk1, Dinosaur Island lab -- need a hint

I am getting an error (probabilities do not sum to 1) after I ravel y.
Please give me a hint on where I could be wrong?

screenshot of the error:

my code looks somewhat like:----

{moderator edit - solution code removed}

Hi @ydsk1234 ,

I see a few things that will need to be reviewed.

First, I am seeing you are using “<=” to assign values.

The “<=” is a logic operator that means “less or equal than”.

To assign a value, you should use “=”.

For example, if you type “a <= 1” you are asserting that a is less or equal to 1.
But if you type “a = 1” then you are assigning the value ‘1’ to the variable ‘a’.

The second item to check is the initial definitions of ‘x’ and ‘a_prev’ - make sure you are initializing them with the right shapes.

Finally, it goes against the code of conduct of this community to post your code. Please remove your code.

Thanks,

Juan

<= is a weird syntax I made up just so people can’t copy-paste the code, i know it’s =, and you won’t see the errors I posted if <= is used instead of =

hope you see the actual error I pasted in the original post

Got it, then check out my second item I mentioned above. You are not initializing x and a_prev properly.

@Juan_Olano
x = np.zeros((Wax.shape[1], 1))
a_prev = np.zeros((n_a, 1))

x is a 1-hot vector and so the second dimension is 1
please give me any more clues…

This is what you got in the first post,

image

But based on your latest post, you have updated the initialization of a_prev, do you still receive the same error message? If so, the message says “probabilities do not sum to 1”, then what does it sum to?

@rmwkwok

I have tried the following:

idx = np.random.choice(range(y.shape[0]), p= np.ravel(y[0])) # same error when p=np.ravel(y[1])

and I get the following error:

ValueError: ‘a’ and ‘p’ must have same size

Hi @ydsk1234

From the error trace above, the problem is in the way how np.random.choice() is called.
For step3, the index of a character is within the vocabulary from the distribution y. So to make the np.random.choicest() call, the parameters needed are the range(vocabulary size), and y, which has to be a 1-D vector, hence the call of y.ravel() or np.ravel(y).

Hi @ydsk1234 ,

Looking at the output generated, the first time going through the while loop, there is no problem in executing the statement:
idx = np.random.choice(range(vocab_size), p= np.ravel(y))
However, on the 2nd time it went through the while loop, valueError is raised because there is a mismatch of a and p.
Within the while loop, x is changed, and x is used in the calculation of a that eventually produced y. And the offending line is :
x = np.zeros(Wax.shape[1])
making x a 1D array of vocab_size without specifying what that array is. In step 1 of your code,
x = np.zeros((Wax.shape[1], 1)), you specified x is a column vector of vocab_size number of elements. That change in x is causing the problem.

To make the code easier to read, may I suggest you use vocab_size consistently across the code.

Here is a post on vector written by one of our mentors that you may find useful.

using vocab size for shape consistently solved the problem … thank you.

Hi @ydsk1234 ,

Which variables have you changed to use vocab_size?