Week 1 Dinosaur Island : sample Step 4 : representing x as one-hot vector corresponding to character prediction

Hello - i am struggling with the syntax to get x represented as a one-hot vector with the entry corresponding to predicted idx set = 1.
any suggestions for the right one-hot syntax welcome:
my code -which doesnt work - is below:

in sample(parameters, char_to_ix, seed)
63 # Step 4: Overwrite the input x with one that corresponds to the sampled index idx.
64 # (see additional hints above)
—> 65 x = np.eye(idx)
66 x[idx] = x[indices]

IndexError: arrays used as indices must be of integer (or boolean) type

x[idx] should be set to 1.

And I think you do not need np.eye() at all.

Hello - note that my question is how to create a one-hot vector for x with the sampled idx.

for your suggestion x[idx] to be set equal to 1 - that throws a syntax error -
TypeError: ‘NoneType’ object does not support item assignment

ALL my struggles are about python syntax - sorry but even if I understand the concepts well, all my struggles are about the syntax, sorry but i dont know python well :slightly_frowning_face:
thanks for any help, Ramesh

In Step 4, you did not complete the code for the x = … statement. Anywhere that you see “None” in the Notebook, you need to replace “None” with your code.

You need to set it to a vector of zeros, of size (vocab_list,1). Then you set the ‘idx’ element to 1.
That is how you create a one-hot vector.

1 Like

Also, please edit your post to remove your code statements. Posting your code breaks the course Honor Code.

Thanks Tom Mosher – your suggestions worked!,

Btw, I tried to delete the code in my post but could not I have flagged for Staff attention. Please feel free to remove any content code that does not fit rules.