Course5 week1 assignment 2

In def Sample part, I received the following error, which I don’t know why.

indices.append(idx)
^
SyntaxError: invalid syntax

When I ran the test, I received the following error:
ValueError: probabilities do not sum to 1

Any hint?

You filed this under General Discussion, so we don’t know for sure which specialization you are talking about. I’m guessing it’s DLS Course 5.

But that type of syntax error at the beginning of a line means one of two things;

  1. The indentation is incorrect. Indentation is part of the python syntax, but in my experience the error message will say “indentation” in that case.
  2. There is something incomplete about the previous line of code, e.g. a missing close parenthesis.

The notebook editor is “syntax aware”: you can click on a parenthesis or bracket and it will highlight the matching one,. Or not :nerd_face:

Thank you! Yes it is DLS Course 5. You are also right that a close parenthesis was missing from the previous line of code.

After I corrected it, I still got the “ValueError: probabilities do not sum to 1” message, pointing to idx = np.random.choice(range(len(y.ravel())), p = y.ravel())
I double checked the three formulas for a, z and y, and they look right to me.

What’s the possible problem here?

Thanks again!

There must be something wrong with your code. That particular error means that your y value is not correct. It should be the output of softmax, right?

Your first argument to np.random.choice is also incorrect, but I don’t think that is what is causing that particular error message. The point is you are randomly choosing a value that is an index into the complete vocabulary.

I changed the shape of x, y, and a from 1-dimensional array of size (vocab_size) to a 2-dimensional array of size (vocab_size, 1) and it passed.
But I don’t understand why a 2-D array is needed - in theory, x and y can be 1-D column array of size 27, right?

2 Likes

The confusion comes from a unique vector handling in numpy, I think.

I changed the shape of x, y, and a from 1-dimensional array of size (vocab_size)

If you initialize x like np.zeros((vocab_size,)) then, x is a vector. You see its shape is (vocab_size,). It’s a column vector and its size is “vocab_size”. If you initialize with np.zeros((vocab_size,1)) then, it is a 2D array. This difference causes several problems. I wrote some details in this thread.

As all parameters passed to this function is 2D array. So, your approach is right. Reshaping vectors into 2-D array is the better way to avoid unnecessary broadcasting.

2 Likes

Thank you very much! The thread is very helpful.