One error failed the whole assignment

Hi! I am encountering a problem of not being able to get properly graded. In the course 5, week 1’s second coding assignment, Character-Level Language Modeling, it threw me the error:

Cell #9. Can’t compile the student’s code. Error: ValueError('operands could not be broadcast together with shapes (4,) (6,) ')

I am wondering why this time a single error failed the whole assignment. It got score 0.

Second, I suspected that the error was because of the calculation of idx in the while loop (in cell 9). it asks to Sample the index of a character within the vocabulary from the probability distribution y, but y is a 2d array in the shape of (vocab_size, n_a), however, the function np.random.choice required the parameter p to be a 1d vector for indicating the probability. I am wondering if anyone can share some idea on this ?
the command I use was:

idx = np.random.choice(list(range(vocab_size)), p = y[:,-1])

  • as you can see, i just randomly took the last column (vocab_size,1) of y as the probability distribution. but i think that’s incorrect
1 Like

Check the additional hint given to you. And, I don’t think you have to convert the range into a list.

Additional Hints
Documentation for the built-in Python function range
Docs for numpy.ravel, which takes a multi-dimensional array and returns its contents inside of a 1D vector.
arr = np.array([[1,2],[3,4]])

[[1 2]
 [3 4]]
[1 2 3 4]

If still facing any issue, share the full error.

1 Like

Hi! Thanks for the reply! I knew that by arr.ravel() a 2D array can be transformed to 1D vector, but the size of the flattened y (27*100,1) will be incompatible with size of vocab_size (27,1), and consequently caused error in np.random.choice. so I doubted if y was calculated properly with size of (27,100)? I printed their sizes shown below:

And my code here:

Moderator Edit: Solution Code Removed.

See these additional hints from the Notebook:

  • x^{<1>} is x in the code. When creating the one-hot vector, make a numpy array of zeros, with the number of rows equal to the number of unique characters, and the number of columns equal to one. It’s a 2D and not a 1D array.
  • a^{<0>} is a_prev in the code. It is a numpy array of zeros, where the number of rows is n_a, and number of columns is 1.

Same mistake in Step 4.

1 Like

Saif has covered the actual programming questions, but the point about why you get 0 points on everything is that you have a compile error. That means the grader can’t get far enough to run any actual tests, since the notebook doesn’t even compile. Anytime something like that happens, you end up with 0 score on everything.

Back to the actual question, here’s a thread which shows the shapes of everything on that particular function. Maybe that will give some clues about where your code goes off the rails. :nerd_face:

Hi Paulinploalto,

Thanks for the explanation! I was thinking the same thing. But how come the grader failed in compiling? is this because of the error in coding and it caused the compiling failures or soemthing else? :joy:

that saved everything!

Thanks bud.

But i would like to inform you that I went through these hints several times and I had to say that i didn’t find these two bullets points about the configuration of the x and a_prev! Below was what shown in my notebook. weird

You have to click on the green text: Additional Hints: Matrix multiplication with numpy.

If your code makes the grader crash via a runtime error, it cannot complete grading your assignment.

Hi @Jerry-liu,

The hints Saif pointed out to are present in the notebook. They are behind the drop down menu, you have to click on the “arrow” to expand them

1 Like

Jerry-liu was using reshape instead of the expected use of ravel in Ex 2 . Making the change fixed the issue.

Hi all. I have one remaining question about storing the course material in my local computer. I am wondering if I would still be able to download them after i finished all the graded stuff (I didn’t subscribe the memberships, but rather I applied for financial aids)? because it says to download them before finishing all graded materials. thanks! :smiley:

Once you complete a course, you no longer have access to the graded assignments (the graded quizzes and the graded programming labs).

So you should download your work before you complete the course.

1 Like

Thank you!