# Course 5, Week 1, A2, Sample function exercise

Hi, my output sampled indices does not match the expected output (only the first 4 indices are correct), can you provide some hints what is wrong in my implementation? Thanks.

Here is my output:

## Sampling: list of sampled indices: [23, 16, 26, 26, 19, 25, 4, 1, 15, 6, 4, 25, 23, 23, 10, 14, 16, 12, 11, 3, 20, 16, 22, 23, 14, 15, 6, 9, 26, 2, 6, 1, 26, 11, 2, 21, 0] list of sampled characters: [‘w’, ‘p’, ‘z’, ‘z’, ‘s’, ‘y’, ‘d’, ‘a’, ‘o’, ‘f’, ‘d’, ‘y’, ‘w’, ‘w’, ‘j’, ‘n’, ‘p’, ‘l’, ‘k’, ‘c’, ‘t’, ‘p’, ‘v’, ‘w’, ‘n’, ‘o’, ‘f’, ‘i’, ‘z’, ‘b’, ‘f’, ‘a’, ‘z’, ‘k’, ‘b’, ‘u’, ‘\n’]

AssertionError Traceback (most recent call last)
in
19 print("\033[92mAll tests passed!")
20
—> 21 sample_test(sample)

in sample_test(target)
15 assert indices[-1] == char_to_ix[’\n’], “All samples must end with \n”
16 assert min(indices) >= 0 and max(indices) < len(char_to_ix), f"Sampled indexes must be between 0 and len(char_to_ix)={len(char_to_ix)}"
—> 17 assert np.allclose(indices[0:6], [23, 16, 26, 26, 24, 3]), “Wrong values”
18
19 print("\033[92mAll tests passed!")

AssertionError: Wrong values

Expected output

Sampling:
list of sampled indices:
[23, 16, 26, 26, 24, 3, 21, 1, 7, 24, 15, 3, 25, 20, 6, 13, 10, 8, 20, 12, 2, 0]
list of sampled characters:
[‘w’, ‘p’, ‘z’, ‘z’, ‘x’, ‘c’, ‘u’, ‘a’, ‘g’, ‘x’, ‘o’, ‘c’, ‘y’, ‘t’, ‘f’, ‘m’, ‘j’, ‘h’, ‘t’, ‘l’, ‘b’, ‘\n’]

Do you still need help with this question?

Hi, I found an error in my implementation, it is ok now. thanks.

Hi I have very similar problem. How did you find the error in your code? Could you give me a hint? Thank you very much!

list of sampled indices:
[23, 16, 26, 26, 19, 1, 4, 17, 23, 6, 21, 22, 14, 16, 9, 19, 24, 6, 7, 18, 9, 14, 8, 12, 4, 21, 16, 21, 17, 2, 8, 26, 11, 0]
list of sampled characters:
[‘w’, ‘p’, ‘z’, ‘z’, ‘s’, ‘a’, ‘d’, ‘q’, ‘w’, ‘f’, ‘u’, ‘v’, ‘n’, ‘p’, ‘i’, ‘s’, ‘x’, ‘f’, ‘g’, ‘r’, ‘i’, ‘n’, ‘h’, ‘l’, ‘d’, ‘u’, ‘p’, ‘u’, ‘q’, ‘b’, ‘h’, ‘z’, ‘k’, ‘\n’]

hi , I got this too. And I wonder if it is wrong at my Step 4:

``````    # Step 4: Overwrite the input x with one that corresponds to the sampled index `idx`.
x[idx] = 1

# Update "a_prev" to be "a"
a_prev = a
``````

Should I also make use of y ?

Oh I have solved this as it is because I miss reset x to zeros before set x[idx]=1.

1 Like

glad you solved this. I solved this with x[idx]=1

hi Tmosh,
could you pls suggest why my error show below? x[idx] = 1 is not the correct way for x override?

TypeError Traceback (most recent call last)
in
19 print("\033[92mAll tests passed!")
20
—> 21 sample_test(sample)

in sample_test(target)
7
8
----> 9 indices = target(parameters, char_to_ix, 0)
10 print(“Sampling:”)
11 print(“list of sampled indices:\n”, indices)

in sample(parameters, char_to_ix, seed)
64 # (see additional hints above)
65 x = 0
—> 66 x[idx] = 1
67
68 # Update “a_prev” to be “a”

TypeError: ‘int’ object does not support item assignment

Perhaps you have not initialized ‘x’ correctly in the previous line.

The instructions for Step 4 tell you to create a “one-hot” vector. To do that you first create a vector of zeros, then set one element to 1.