DLS 5, Week 1 assignment 2, Ex 2 choice parameters a and p are not the same size

hey I’m getting this error on the choice part of the code:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-78-a13d660e654a> in <module>
     19     print("\033[92mAll tests passed!")
     20 
---> 21 sample_test(sample)

<ipython-input-78-a13d660e654a> 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)

<ipython-input-77-323a9e4e9818> in sample(parameters, char_to_ix, seed)
     58         # Step 3: Sample the index of a character within the vocabulary from the probability distribution y
     59         # (see additional hints above)
---> 60         idx = np.random.choice(list(range(0, vocab_size)), p = y.ravel())
     61 
     62         # Append the index to "indices"

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

ValueError: 'a' and 'p' must have same size

y have a shape (27, 100)

is the problem in how I’m calling choice or the problem is in the y calculation?

The shape of your y value is wrong. I added a bunch of print statements to my code to show the sizes of things. Here’s what I get:

vocab_size = 27
Wax (100, 27) x (27, 1) Waa (100, 100) a_prev (100, 1)
Wya (27, 100) x (100, 1) + by (27, 1)
y.shape (27, 1)
len(y) 27
len(y.ravel()) 27

Comparing the sizes you get should give you a pointer to where things go off the rails.

1 Like

Thanks! That helped a lot!