Week 1, Assignment 2, Random Choice

Hi am totally stumped how to use a combination of ravel and np.random.choice to draw an index in 0 to 27, from an array of probabilities of shape (27,100).

The best I’ve been able to come up with is:

mentor edit: code removed

But this is clearly wrong.

Any help on this silly bit of the assignment would be really appreciated

In the future, please do not post your code on the Forum. That breaks the course Honor Code.

The indices use np.random.choice(…) with two arguments:

  • one specifies the vocabulary size,
  • the ‘p’ argument comes from using ravel() on y.

Your problem is that the shape of y should be (27,1) and not (27,100). That is probably what is causing the difficulty with np.random.choice. I added some print statements to show the dimensions of everything. Comparing to that may give some clue as to where your code goes off the rails:

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
type(y.ravel()) <class 'numpy.ndarray'>

Thanks for responding @paulinpaloalto

I don’t understand what I am doing wrong in the z = Wyaa + by step then, using np.dot. Wyaa is a matrix of size (27,100). And softmax doesn’t change the size of the input. So what am I doing wrong here?

In setting a_next (just a in the code) Waa .dot. a_prev is a 100x100 matrix. … what the heck am I doing wrong?

Did you look at the dimensions that I showed above? If you dot (27, 100) with (100, 1), you end up with (27, 1), right? So why did that not happen in your case? I’ve given you the information, now you need to do some thinking. Your a is the wrong shape. I suggest you compare your code for computing a to the mathematical formula given in the instructions.

Yes I did. My point was my math wasn’t coming up with the same dimensions.

I traced the problem back to initializing a_prev as a vector of size (n,) rather than (n,1). SOlved

Interesting. Yes, dot products get weird when you start mixing 2D and 1D objects.