Assertion error in get_disc_loss C1W1


I am having errors with get_disc_loss with this line specifically assert (disc_loss - 0.68).abs() < 0.05

All of the previous tests for the builder functions passed and my code for get disc_loss is like that

  • Generate noise with the batch length and the dimension of the noise
  • Pass that noise through the generator and detach (all in one line)
  • Pass the fake images through the discriminator to get the predictions
  • Create a tensor of labels 0 of sizes (num images ,1)
  • Calculate the fake loss with criterion, passing both the labels and the predictions
  • Pass the real images through the discriminator and get predictions
  • Create a tensor of labels 1 of sizes (num_images, 1)
  • Calculate the real loss with criterion
  • Sum the fake and the real loss, average them and divide them by 2 and return

Apparently I am doing something wrong, but I can not seem to find where. Can anybody help?

1 Like

Hi Luis_Fernandez!
Welcome to the community :dizzy:.
Your implementation logic is correct! So I’m not sure what’s causing that error. Maybe I shall check the implementation, so please share the code snippet of that cell with me through private chat, let me look at it once.

Was there a solution here? I am stuck on the same assertion test. I am following the same logic and get stuck on the same step.

Hi Andrew!
Welcome to the community :dizzy:.
In your case, there is no mistake in your implementation. I have cross-checked your code (which you sent me in private chat) by running it in my notebook too, and it works fine! I’m afraid that there might be some other issue. I request you to share your notebook through private chat.

There is no error in his code and it seems that it is working for Andrew too!

1 Like

I am getting the same error !! can you please help me

Hi Hitarth!
Welcome to the community. I hope you are doing well. In this case, apparently, the learners did not have any errors in their implementation. It was strange as it was throwing an assertion error for Andrew, but when he restarted the kernel and when he ran all cells again it worked for him.
Can you elaborate more on the issue you are facing so that we can help you?

I basically get the same error as posted here! I tried to run it again and it still doesnt work. Please help me immediately because I have to finish this assignment tomorrow!

Hey Hitarh, sorry for the late reply. I was off for a few days. If the issue is still persisting then kindly share your notebook with me through Private Chat. I will check what’s happening in your case.

I have trouble to understand the nature of the assignment of week 1 and week2. We are given the python jupyter notebook and the code, and then there are errors - known as assertion error in
the generator block:

AssertionError Traceback (most recent call last)
Input In [28], in <cell line: 2>()
----> 2 assert tuple(hidden_output.shape) == (num_test, 20, 4,4)
3 assert hidden_output.max() > 1
4 assert hidden_output.min() == 0


Question: Are we going to change the combination of (20,4,4) to make it executable by python.

Also at the bottom, similar in the discriminator block:

AssertionError Traceback (most recent call last)
Input In [22], in <cell line: 2>()
1 # Test the hidden block
----> 2 assert tuple(hidden_output.shape) == (num_test, 5, 8, 8)
3 # Because of the LeakyReLU slope
4 assert -hidden_output.min() / hidden_output.max() > 0.15


How should I get start for the assignment ?

Tom Ho

Hi Tom!
I hope you are doing well.

The test cases given in the notebook need not make sense with respect to the context. It is just given to check the implementation of our code (whether we have included all the necessities and have correctly written the code). Apart from that, the test cases don’t mean anything. So please don’t worry about the test cases, if it is throwing errors then it means that your implementation is not correct (make sure that you have included all the layers as given in the instructions).
Please feel free to post if you still face any issues.


@tom_tom1, to add a little more explanation about the unit tests - as @Nithin_Skantha_M explains, they are there to test your code to help you find problems early-on before you continue on to the next step.

The way unit tests generally work is that they call your function with a specific input and then check if the output is what is expected for that input. The assert statement is what checks if the output is what expected. If it’s not, it will raise an assertion error. Your job at that point is to go back and look at your implementation to figure out why it is not returning the expected result for the given input.

Same here.
I passed the test few hours ago. But I came back from my walk, it will never pass​:thinking:

I’m not clear. Is there a problem you’re still seeing that you need help with? If so, can you share the error you’re getting, including the full stack trace?

@Wendy Sorry for late reply.

Short answer: No I don’t need help with this problem. But this issue should be fixed for future learners.

Long answer: I passed this test few days ago and got Code Cell UNQ_C6: Function 'get_disc_loss' is correct.. Even though I haven’t change the code, I got the below error when running test_disc_loss().

AssertionError                            Traceback (most recent call last)
Input In [16], in <cell line: 74>()
     71                         break
     73 test_disc_reasonable()
---> 74 test_disc_loss()
     75 print("Success!")

Input In [16], in test_disc_loss(max_tests)
     52 # Calculate discriminator loss
     53 disc_loss = get_disc_loss(gen, disc, criterion, real, cur_batch_size, z_dim, device)
---> 54 assert (disc_loss - 0.68).abs() < 0.05
     56 # Update gradients
     57 disc_loss.backward(retain_graph=True)


Luckily, I have downloaded the notebook(test passed one). So I uploaded it and ran it but failed…

Finally I tried to refresh the notebook by following this instruction but it throws another error below. (I can refresh other notebooks like DCGAN’s one.)

Traceback (most recent call last): File “/usr/local/lib/python3.8/dist-packages/send2trash/”, line 17, in send2trash f.trash(cancellable=None) gi.repository.GLib.GError: g-io-error-quark: Unable to find or create trash directory for /tf/C1W1_Your_First_GAN.ipynb (0) During handling of the above exception, another exception occurred: Traceback (most recent call last): File “/usr/local/lib/python3.8/dist-packages/tornado/”, line 1704, in _execute result = await result File “/usr/local/lib/python3.8/dist-packages/tornado/”, line 234, in wrapper yielded = ctx_run(next, result) File “/usr/local/lib/python3.8/dist-packages/notebook/services/contents/”, line 239, in delete yield maybe_future(cm.delete(path)) File “/usr/local/lib/python3.8/dist-packages/notebook/services/contents/”, line 279, in delete self.delete_file(path) File “/usr/local/lib/python3.8/dist-packages/notebook/services/contents/”, line 531, in delete_file send2trash(os_path) File “/usr/local/lib/python3.8/dist-packages/send2trash/”, line 23, in send2trash raise OSError(e.message) OSError: Unable to find or create trash directory for /tf/C1W1_Your_First_GAN.ipynb

It seems I’ve hit a dead end. Since I have passed the test, I don’t need to re-pass the test.

Thank you for your support.

Thanks, @peter4, for pointing this out!

I’ll let the staff know that file delete does not work for this notebook so they can get it fixed.

In the meantime, as a work-around, instead of deleting the file, you (or anyone else who runs into this issue) can just rename it. Once you’ve renamed it, you can add ?forceRefresh=true as suggested in the instructions, or you can click the “? Help” button in the upper right corner, and then choose “Get latest version”

1 Like

Thank you so much @Wendy !

I succeeded to refresh the notebook using the renaming trick​:sunglasses:, and re-passed the test.

1 Like