C4 W4 A2 - P2 Art Generation E6 train_step (UNQ_C5) "Unexpected cost for epoch"

I’ve reviewed at least 6 or 7 posts on this AssertionError and have tried all suggested fixes including:

  • tf.square or **2
  • tf.substract or -
  • checking that various cost function calls receive correct content, style, and generated arguments in the correct position
  • Kernel > Restart & Run All
  • closing browser, shutting computer down, going for walk
  • repeating the above in various permutations in the same or different sessions

All tests for the assignment are passed (and the model trains/works, even on my own content and style photos), but the unit test for UNQ_C5 – it gives the following error

AssertionError: Unexpected cost for epoch 0: 24085.95703125 != 25629.055

Given that the just below that cell, the “Expected output” for the 1st tensor is

tf.Tensor(25700.346, shape=(), dtype=float32)

the output of my code is close, but how can I get it closer/pass the unit test?


{reply edited by myself}

Following the instructions on the site, I renamed the python notebook assignment and then got the latest version of the assignment, a clean version.

I had downloaded my completed but error-generating version as html and just pasted the unicode text from it into the new, clean version–error gone, I got 100% on the submitted assignment.

Might a been a hidden “global” var I whacked partway through the assignment that wasn’t in the clean version.

I don’t know what I’m doing wrong here. I’m getting “unexpected cost for epoch”, but my cost is quite a bit off.

Can someone help me?
I also tried re-downloading the notebook again. Still same error and same cost.

I got the same error when I did this assignment in my local computer with a different version of Python, TensorFlow, etc. from the Coursera environment. Are you using the local machine or the Coursera environment?


I’m doing it in the Coursera environment. Not in the local machine.

It’s strange. I am also getting this error in the Coursera environment. Cannot figure out what is wrong here. But my submission is 100.

What is the meaning of that “Last cell”? The last cell of the notebook frees up the resources but here “Last cell” is referring to any other cell. So, where it is?

So I can go ahead and submit it, despite the error? :thinking:

Go ahead and try this…

Tried it. Got 0/100. :worried:

Let’s wait for some other mentor to reply.

wait a minute. Have you changed the images?
5.1 Load the Content Image and 5.2 Load the Style Image?

I changed these and that is why I was getting a different cost.

1 Like

No. I haven’t changed the images. I’m using “images/louvre_small.jpg” for content, and “images/monet.jpg” for style.

Then maybe you’ve changed content_layer = [('block5_conv4', 1)] or number of iteration in optimizer = tf.keras.optimizers.Adam(learning_rate=0.01). Check it out.

If you didn’t change anything, try Kernel → Restart & Clear Output. Then run all the cells again. Let’s see if it solves the issue.

No. They are all the same.

Have done these multiple times. No change.

The only thing that seems to bring the cost close to the expected one is by tinkering with the noise value. The default is -0.25, 0.25, if I’m not mistaken. Changing it to -0.22 and 0.22 brings the cost to 27474. Don’t know what else to do.

Send me your code of train_step in a private message. But make sure you didn’t change anything. If you did, revert it to the original. Click my name and message.

It means “the previous cell”.

1 Like

It’s a normal practice that we have to run a previous cell before the test cell. I don’t know why they mentioned that. And the wording is vague, “Last cell” confuse me with the last cell of the notebook.

I ran my copy of the notebook (the March 8 2023) version without any errors.
I do not know what’s different in the April 4th version.

I find it’s always challenging to debug this notebook, I’m not sure why.