Strange error on unit test of Exercise 3 'answer_question' function for Assignment 3: Question Answering

Is there a way to resolve this error?

I am working on Exercise 3 (the ‘answer_question’ function) in Assignment 3: Question Answering–the very last assignment in the NLP Specialization. I have completed the code for the exercise (it’s not shown here), and I think it is correct, but the standard unit test in the very next cell in the notebook (after one line of text) is giving me a strange error (see image below). (Don’t worry, none of my code is posted here–it’s just the standard unit test that appears in every notebook for this assignment, along with the error).

It seems there might be an issue with the ‘decoder_token_ids’–perhaps in the utils function? Apparently, it is supposed to be a TensorFlow tensor–probably tf.int32 or tf.int64, with a shape of (batch_size, sequence_length), according to my research.

Does anyone know what is causing this strange error and how I can fix it? Perhaps there is a problem somewhere else in the code?

Currently, the grader has given me 70 points (a 70% score) on this assignment (the other two functions appear to work exactly as intended). You need at least 75% to pass this assignment, so until I get this resolved, I’m out of luck.

Please provide suggestions. An image of the unit test and error are shown below. Thanks!

Just to highlight it, the text says:

TypeError: in user code:

File "/tf/", line 547, in create_padding_mask  *
    seq = 1 - tf.cast(tf.math.equal(decoder_token_ids, 0), tf.float32)

TypeError: Failed to convert elements of <transformer_utils.Transformer object at 0x7f890c937b20> to Tensor. Consider casting elements to a supported type. See for supported TF dtypes.
1 Like

There should be something wrong with the code here I guess! Can you send me in private the code for exercise 3? I will check it later on!

1 Like

Thank you for your response!

I finally figured it out. If anyone sees the same error, I recommend tinkering with two lines in Exercise 3 (‘answer_question’) until they resolve it: The code directly below two comments in Exercise 3:

  • ‘Get the id of the EOS token’ (consider how this was done earlier in the notebook)
  • ‘The text generation stops if the model predicts the EOS token’ (checking ‘next_word’ is a little tricky)

I don’t think I had to change anything else in Exercise 3 apart from replacing the ‘None’ values as best I could according to the instructions.

I’ve since received full credit for the assignment and the NLP specialization!



Can you send me the code of your changes? I am facing the same problem.

1 Like

thank you

1 Like

Could you please privately send me the changed part of code? I am facing the same problem and debugged for days, but such error just happened recurrently. Thanks a lot!

1 Like

[quote=“ai_developer, post:3, topic:533803”]
finally figured it out. If anyone sees the same error, I recommend tinkering with two lines in Exercise 3 (‘answer_question’) until they resolve it: The code directly below two comments in Exercise 3:

  • ‘Get the id of the EOS token’ (consider how this was done earlier in the notebook)
  • ‘The text generation stops if the model predicts the EOS token’ (checking ‘next_word’ is a little tricky)

I am getting the same error I could not understand what you are saying. Please guide me almost all getting this error

1 Like

Hi guys!

It looks like I wasn’t the only person who found this part very challenging to troubleshoot, so I’ll offer an additional hint on how to resolve the error I posted about.

In my case, in Exercise 3 of the C4W3 assignment, I had to modify the provided template code. They gave us “eos = tokenizer.string_to_id(”“)” in an area that was not specified for modification; I had to append “.numpy()” to the end of this. So later, to detect a token, I had to use “next_word[None, None].numpy()”. I think this solved the error I was seeing. (You have to replace the two ‘None’ values with something that makes sense).

I hope this helps!

1 Like

I am facing the same error. Could you please send it to me?

Students, please note:

Asking another student to send you their code is not allowed by the Code of Conduct.

1 Like

Another best practice–if you are struggling with this code–or any code, for that matter–is to download it and run it in on your local computer using VS Code 2 while leveraging the included ‘Debugger’ tool. By setting breakpoints at key lines in the code (e.g., in the function you are trying to troubleshoot), you will be able to observe what value and datatype each variable takes as the function runs.

There are instructions in each course on how to download all of your code for a lab (including the dependent modules–such as utility functions and ‘helper’ code–that reside elsewhere in the directory tree); they tell you how to download everything as a zip file using a single click.

It’s definitely worthwhile to figure out how to install VS Code and use the Debugger if you don’t already know how to do so. To learn that, you’ll have to look online and follow instructions for your specific operating system. It’s actually not that hard to do.

Remember that for your code to run properly (with the help of the other modules), the modules will have to be at the same relative position in your directory tree as they were when you ran the lab on Coursera. This relative positioning should be taken care of automatically, since when you download ALL of the code for a single lab (with the hidden modules) as one file, all of the dependent modules should get included in the zip file automatically (often as sub-directories). Just follow the download instructions to look around the directory tree in your Jupyter notebook before you download your zip file so you know what is there. That way, you’ll know where things need to be when you run your notebook in VS Code. If you notice something odd as you run the debugger, that’s probably the thing you need to fix to get your code to run correctly.

If using VS Code and its debugger seems too complicated for you, you could alternatively try the free online debugger tool located at ( This tool enables you to paste your code into a window, and then it will help you analyze the code visually as it runs to help you understand the execution and spot any problems. Note: I DO NOT recommend pasting your company’s proprietary code into this tool, as that action would expose the code–but for learning Python and analyzing open source code, it should be fine. This tool supports Python, Java, C, C++, and JavaScript.

Good luck!


Be careful using VSCode - there is some evidence that it can mangle the notebook metadata in a way that makes the grader unhappy, if you put your notebook back into Coursera Labs for grading.

Note also that at some point, Juypter is going to update to include debugger tools. That will be very handy.

To add my penny: if you print out eos it’s a tf.Tensor(1, shape=(), dtype=int32)
while next_word is a tf.Tensor([[…]], shape=(1, 1), dtype=int32)
So the minimal solution, in my opinion, is to do tf.squeeze(next_word) in the stopping condition.

Just a snippet from this, to download all these labs, is there any repository that one can easily reference the used pre-trained models and data for all the modules of the course?