DLS C5 W1 A2 - Help with ValueError: 'a' and 'p' must have same size

I am working on the code for the function ‘sample’ in A2 of W1. I cannot make it work because of different sizes in vocab_size and p (27 vs. 2700). I tried to troubleshoot it several times, but I am really stuck. This is the code I implemented:

codes removed by moderator as it is against community guidelines.

Any help will be greatly appreciated. Thanks in advance.

Hi @gugasparini, your code was removed by a moderator as it is against community guidelines. But you can share a screenshot of your complete error so that the mentors can help you out.

Apologies, I was not aware of that.
Please see below the screen shot of the error message:

I have double checked sizes of all vectors and matrices and do not know how to bring p (p=y.ravel()) and vocab_size to be the same size so that idx can work

Once again, appreciate any help. I am really stuck

Where you have “p=y”, please read this hint from the instructions:

If this doesn’t solve the issue:
…(range(vocab_size), p=y.ravel())

Then the next place in your code to check is Step 2, where you compute ‘a’, then ‘z’, then ‘y’.

ALERT: Your code in Step 2 should not compute a new ‘x’ value.

In addition to Tom’s points, here’s a thread which shows the sizes of the various values in this function. Please compare your results to what you see there and maybe that will give some insight.

Ok, I did some experiments based on the code that you showed. Your mistake is that you have created x as a 1D vector with shape (vocab_size,). It must be a 2D vector with shape (vocab_size, 1).

The way various operations work when you mix 2D and 1D vectors is different and can be surprising (in a bad way :scream_cat:).

One other note is that they gave you a softmax function, so you don’t have to write that out yourself: you can just call it.

Thank you @paulinpaloalto very much for your advice. That was indeed the problem. I corrected it and the code ran successfully!
Really appreciate it! Thank you @lukmanaj and @TMosh for your help as well!

1 Like