DLS Course 5, week 1, assignment 2: samples

I got stuck in the excise 2 “samples”. I got the following massage:

TypeError: object of type ‘builtin_function_or_method’ has no len()

Even though I changed the code w/o len() for debugging, I still go the same error above. It seems to me that the old code was run instead of new code. Could you help me?

in concrete:
I added one line code yl= len(y) for debugging and then delete it. But I still get the following message:

57 # (see additional hints above)
58 yl = len(y)
—> 59 idx = np.random.choice(range(yl), p = y.ravel)
60
61 # Append the index to “indices”

mtrand.pyx in numpy.random.mtrand.RandomState.choice()

TypeError: object of type ‘builtin_function_or_method’ has no len()

In the test code I just wrote range(10) instead of (yl), but still go above message.

In new code I just wrote range(10) instead of yl.

Hi @Jade

it should be vocab_size not yl, because we are sampling a char within the vocabulary.

And I think you should try restarting the kernel.

Thank you very much for your hints! I restarted the kernel and changed the code. I don’t have len in the code any more but still got the following message:
60 idx = np.random.choice(range(vocab_size), p = y.ravel)
61
62 # Append the index to “indices”

mtrand.pyx in numpy.random.mtrand.RandomState.choice()

TypeError: object of type ‘builtin_function_or_method’ has no len()

Hi @Jade

y.ravel is a method, so there should be a pair of parenthesis:
range( vocab_size), p=y.ravel()

and the complete statement is:
idx = np.random.choice(range(vocab_size), p = y.ravel())

1 Like

that is exactly what I wrote.

But your code is like this:
idx = np.random.choice(range(vocab_size), p = y.ravel)
which is not the same as
idx = np.random.choice(range(vocab_size), p = y.ravel())

Thank you very much! It seems worked out. But the next error is waiting for me. :frowning:
I think that since I initial x with zeros in np.zeros((vocab_size, 1)), I could fill the x[idx] with 1. Apparently it is not correct. I tried to use [numpy.ndarray.fill], but could not find the correct function. Could you help me? Thank you very much!
error message:
68 x[idx] = 1
69
70 # Update “a_prev” to be “a”

IndexError: list assignment index out of range

Hi @Jade ,

The error message suggested that there is a problem with the value of idx. So I suggest you use a print statement to print out the value of idx, that would show you where the problem might be.

Thank you very much! Now I get the message
AssertionError: Wrong values

My idx results (after added debug line print idx as you suggested)
Sampling:
list of sampled indices:
[23, 16, 20, 26, 18, 17, 7, 0]
list of sampled characters:
[‘w’, ‘p’, ‘t’, ‘z’, ‘r’, ‘q’, ‘g’, ‘\n’]

Hi @Jade ,

The first 6 indices should be: [23, 16, 26, 26, 24, 3]
So you need to trace why the indices started to go wrong from the 3rd index. I suggest you have a look at the code for the forward propagation, and make sure your code follow equation 1, 2 & 3.

Thank you for your hint. Now I get the following result and could not find the error in the 3 equations.
Sampling:
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’]

I listed the result which started going wrong. Since I don’t know how the function random.choice works, I can’t debug it anymore
counter 4
idx 19
y (27, 1) [[1.32533718e-04]
[6.52366520e-06]
[2.80096358e-08]
[6.23085232e-10]
[1.98917046e-07]
[1.36752511e-14]
[2.55792679e-05]
[7.19586263e-07]
[9.72758201e-12]
[2.21811310e-12]
[6.92217301e-10]
[1.45452723e-07]
[1.01977302e-05]
[1.25077390e-02]
[1.00986585e-03]
[2.54910862e-10]
[1.32579061e-07]
[1.44228282e-05]
[8.02695890e-01]
[6.60239162e-02]
[1.99187402e-06]
[2.65676962e-05]
[5.55784695e-08]
[1.45062743e-05]
[1.17362546e-01]
[1.66438784e-04]
[2.24615312e-11]]
x [[1.32533718e-04]
[6.52366520e-06]
[2.80096358e-08]
[6.23085232e-10]
[1.98917046e-07]
[1.36752511e-14]
[2.55792679e-05]
[7.19586263e-07]
[9.72758201e-12]
[2.21811310e-12]
[6.92217301e-10]
[1.45452723e-07]
[1.01977302e-05]
[1.25077390e-02]
[1.00986585e-03]
[2.54910862e-10]
[1.32579061e-07]
[1.44228282e-05]
[8.02695890e-01]
[1.00000000e+00]
[1.99187402e-06]
[2.65676962e-05]
[5.55784695e-08]
[1.45062743e-05]
[1.17362546e-01]
[1.66438784e-04]
[2.24615312e-11]]
counter 5
idx 1

post your notebook in a DM to me, I will have a look for you.

what is a DM? How can I do that?

DM means direct messages. If you click on my icon, you will see a display box with a blue button marked message. Just click on that, and a new dialogue box will be opened. You can type your message there. If you were to post your code to me, that is the preferred way of doing it in order to comply with the honour code policy.

Hello. I am having the same problem, 4th first indices are correct then it is going with wrong values, how did you solved that? I mean, I am having the exact same wrong values as Jade.

Thank you

Hi @Slavo ,

Although your results are the same as Jade, it could be completely different places that have gone wrong. If you put that section of code in a direct message to me, I will have a look for you.

Thank you for your availability, but it is solved now, I didnt realized than I needed to do x = zeros before x[idx].

Hi @Slavo ,

Great to hear you have found the solution. Thanks