C4 W4 A2 - p2 Art Generation - E6 train_step (UNQ_C5)

Hello there!

Analysis

I am having an issue similar to what has been reported back then in May:

Back then it seems that the issues were one of these three possible causes:

  1. Either the square function was not working and needed to be replaced by **2, or
  2. The issue was with the transposing in a previous cell (UNQ_C3)
  3. The Kernel needed to be restarted

I checke / tried these. However, in my case is neither.

Error raised

Tensor("Sum:0", shape=(), dtype=float32)
Tensor("Sum_1:0", shape=(), dtype=float32)
Tensor("Sum_2:0", shape=(), dtype=float32)
Tensor("Sum_3:0", shape=(), dtype=float32)
Tensor("Sum_4:0", shape=(), dtype=float32)
Tensor("Sum:0", shape=(), dtype=float32)
Tensor("Sum_1:0", shape=(), dtype=float32)
Tensor("Sum_2:0", shape=(), dtype=float32)
Tensor("Sum_3:0", shape=(), dtype=float32)
Tensor("Sum_4:0", shape=(), dtype=float32)
tf.Tensor(10221.386, shape=(), dtype=float32)
---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
<ipython-input-50-e8adb96bb73f> in <module>
      5 print(J1)
      6 assert type(J1) == EagerTensor, f"Wrong type {type(J1)} != {EagerTensor}"
----> 7 assert np.isclose(J1, 10221.168), f"Unexpected cost for epoch 0: {J1} != {10221.168}"
      8 
      9 J2 = train_step(generated_image)

AssertionError: Unexpected cost for epoch 0: 10221.3857421875 != 10221.168

Cause

10221.3857421875 != 10221.168

Assertions

There are two test cases, J1 and J2. The difference is really micro-small for test case J1, and the code passes the assert in J2.

J1 = train_step(generated_image)
print(J1)
assert type(J1) == EagerTensor, f"Wrong type {type(J1)} != {EagerTensor}"
assert np.isclose(J1, 10221.168), f"Unexpected cost for epoch 0: {J1} != {10221.168}"

J2 = train_step(generated_image)
print(J2)
assert np.isclose(J2, 6081.23541, rtol=0.05), f"Unexpected cost for epoch 1: {J2} != {6081.2354}"

Could anybody please point here where I can probe further? I am stuck now for a while and this is the only notebook cell raising an assert. Everything else works…

Thanks!!!

It may be just a problem with the grader. I submitted the notebook for grading and got all the available points.

So either the grader is wrong or the asserts are.

The grader tests your code with a different set of data. I suspect there is an error in your code that only impacts the grader’s test case.

The “J” value used in that assert is a little difficult to debug, because that calculation calls a lot of other functions.

Please download your notebook and upload it to me via a private message.

The other potential source of confusion and misdiagnosis here is that the grader does not do an automatic “Save” for you. So WYSINNWTGG (What You See Is Not Necessarily What The Grader Gets). What the grader gets is the content of the notebook as of the last time you clicked “Save”. Note that in some of the earlier courses the grader does do automatic saves, but not here.

Thank you @paulinpaloalto and @TMosh for the pointers.

Update: The grader appears to use a less stringent test threshold than is used by the notebook’s unit test.

Hello, I am getting the same error on the assessment of Exercise 6 - train step. I tried changing the square functions to **2 as suggested above and that didn’t affect the error.

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

AssertionError Traceback (most recent call last)
in
5 print(J1)
6 assert type(J1) == EagerTensor, f"Wrong type {type(J1)} != {EagerTensor}"
----> 7 assert np.isclose(J1, 10221.168), f"Unexpected cost for epoch 0: {J1} != {10221.168}"
8
9 J2 = train_step(generated_image)

AssertionError: Unexpected cost for epoch 0: 2555.3056640625 != 10221.168

There are some factors of 1/4 involved there, right? Notice that 4 * your value is looking pretty close to the expected value. Hmmmmm. :thinking: Maybe worth checking “order of operations” just on general principles.

Hello , I have a similar problem. The grader gave me full marks. Did you find a solution for this discrepancy ? I think the notebook assertion is wrong.

If the assertion failed, but your code passes the grader with a full score, I think it is more likely that this is a bug in the grader. And a bug in your code. The point being that the assertion is correct, so your code should have failed the grader.

I have a similiar problem, but in my case the asserts are ok, but the grader tells me train_step is incorrect and I can’t get all the “points”. Instructions say to use a learning_rate = 0.03 but in the code outside the YOUR CODE GOES HERE section the learning rate is setted at 0.01. If I change this then the asserts fails. I have not tested the grader with the learning rate changed to 0.03, maybe it will pass the test, I’m gonna try it.

EDIT: The grader fails also with lr=0.03… I don’t see where it fails if the other functions pass the grader…

EDIT 2: I’ve changed tf.square to **2 and now it looks like all is correct, the asserts and also the grader. Don’t understand why it fails with tf.square…

They changed the learning rate in the template code to 0.01, but it looks like they forgot to change the instructions. I’ll file a bug about that.

In the case of tf.square, it should work, but with TF you have to be really careful about integer versus float types. All the n_H and so forth are integers, so if you use 1 and 4 as the constants instead of 1. and 4., then you get an integer division and different rounding behavior.

1 Like

Thanks paulinpaloalto,

I’ve already tested casting the results to float before the division, but It stills fails. I’ve also tried now to cast the values before using tf.square and now it gets the right result. Definitely I Will take much more care with data types before using these functions. Thanks again

Please change the learning_rate data because I also got that error and if it is not because I check all the posts I do not find it.

hello,
i am stuck in the E6 train_step and can’t get to understand the error: Can someone help here?

EDITED:
solved.
error was on a_G=vgg_model_outputs(tf.Variable(tf.image.convert_image_dtype(generated_image, tf.float32))) which was wrong and supposed to be simpler

The “no gradients” error usually means you used some numpy functions in the compute graph which breaks the gradient computations.

And is this issue happening when you run on the course website or over on Colab?