Course 2 Week 3 Assignment Exercise 6 - NameError

When trying to calculate the cost function I face the following error NameError: name ‘new_y_train’ is not defined. Did anyone face the same issue before?

Hi @kats23 ,

How did you create new_y_train in the cost function?

It was declared by default on Exercise 3 - one_hot_matrix as follows :
new_y_test =
new_y_train =

also I have the error: NameError: name ‘new_y_test’ is not defined
when running the default print(next(iter(new_y_test))) after the declaration of the above

Hi @kats23 ,

The cost function has 2 input arguments, (logits, labels), these are the variables to be used in coding the cost function. Here is the comment describing the input and out arguments:
logits – output of forward propagation (output of the last LINEAR unit), of shape (6, num_examples)
labels – “true” labels vector, same shape as Z3

cost - Tensor of the cost function

There is compute_cost_test(compute_cost, new_y_train ) which takes 2 arguments after the declaration of compute cost and there I have the issue with name ‘new_y_train’ is not defined
as you can see in the screenshot.

Hi @kats23 ,

That is mainly to do with the execution environment couldn’t find the new_y_train variable that was set early. This can happen when the kernel is out of step after a period of inactivity. for example, you logged off and now start working on the notebook again.

All is needed is:
click the Cell tab on the menu bar at the top,
select run from start.

I have tried your suggestion but that was not the problem. The issue was that I had implemented wrong the one_hot_matrix . I fixed the code with the correct solution and the error disappeared. If you implement wrong the one_hot_matrix you get a misleading error, such as the one mentioned in this topic.

1 Like

Hi @kats23 ,

That is great you have solved the problem. Just wanted to clarify that the error message is not misleading. As I explained, the execution environment couldn’t find variable new_y_train, hence output the error message.
What has happened after you fixed the code and the error disappeared is due to fact that one_hot_matrix() was executed and the following cell where new_y_train was assigned with values returned by

Yep, the same with me, the shape was wrongly defined despite the


Was ok