# Assertion Error in get\_disc\_loss

**URL:** <https://community.deeplearning.ai/t/assertion-error-in-get-disc-loss/239109>\
**Category:** Build Basic Generative Adversarial Networks\
**Tags:** week-module-1\
**Created:** [December 6, 2022, 2:23pm UTC](https://community.deeplearning.ai/t/assertion-error-in-get-disc-loss/239109 "2022-12-06T14:23:50Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Pradeep\_Mohan](https://avatars.discourse-cdn.com/v4/letter/p/51bf81/32.png) [@Pradeep\_Mohan](https://community.deeplearning.ai/u/Pradeep_Mohan)\
**Post date:** [December 6, 2022, 2:23pm UTC](https://community.deeplearning.ai/t/assertion-error-in-get-disc-loss/239109/1 "2022-12-06T14:23:50Z")

</div>

Here is my code for the get\_disc\_loss and I think I am detaching the generator correctly, but I am still getting an assertion error:

_{moderator edit - solution code removed}_

---

<div class="post-metadata">

**Author:** ![paulinpaloalto](https://sea1.discourse-cdn.com/dlai/user_avatar/community.deeplearning.ai/paulinpaloalto/32/187_2.png) [@paulinpaloalto](https://community.deeplearning.ai/u/paulinpaloalto)\
**Post date:** [December 6, 2022, 3:49pm UTC](https://community.deeplearning.ai/t/assertion-error-in-get-disc-loss/239109/2 "2022-12-06T15:49:25Z")

</div>

Note that [`detach()` is not an “in place” operation](https://pytorch.org/docs/stable/generated/torch.Tensor.detach.html): it returns a new instance of the object with the graph detached. So that means you are discarding the detached graph and that `disc_pred_fake` has already been created with the non-detached instance of `gen_img` in any case, meaning that your `detach` happens too late.

---

<div class="post-metadata">

**Author:** ![snc](https://avatars.discourse-cdn.com/v4/letter/s/c77e96/32.png) [@snc](https://community.deeplearning.ai/u/snc)\
**Post date:** [December 29, 2022, 3:56am UTC](https://community.deeplearning.ai/t/assertion-error-in-get-disc-loss/239109/3 "2022-12-29T03:56:01Z")

</div>

I have a similar error. I detached the generator from the predictions before I computed the loss for the fake images (and did not detach for the real images), but I am seeing the same error at runtime.

_{moderator edit - solution code removed}_

The error is as follows -

`RuntimeError: element 0 of tensors does not require grad and does not have a grad_fn`

I tried moving the detach() around but haven’t had any luck. Can you help point me in the right direction?

---

<div class="post-metadata">

**Author:** ![paulinpaloalto](https://sea1.discourse-cdn.com/dlai/user_avatar/community.deeplearning.ai/paulinpaloalto/32/187_2.png) [@paulinpaloalto](https://community.deeplearning.ai/u/paulinpaloalto)\
**Post date:** [December 29, 2022, 4:11am UTC](https://community.deeplearning.ai/t/assertion-error-in-get-disc-loss/239109/4 "2022-12-29T04:11:36Z")

</div>

I think you are doing the detach too late: you are detaching the _output_ of the discriminator for the fake images. The point is you want to detach the fake images _before_ you feed them to the discriminator. The way you are doing it, the discriminator doesn’t have gradients for the fake case, which is not what you want, right?

---

<div class="post-metadata">

**Author:** ![snc](https://avatars.discourse-cdn.com/v4/letter/s/c77e96/32.png) [@snc](https://community.deeplearning.ai/u/snc)\
**Post date:** [December 29, 2022, 4:16am UTC](https://community.deeplearning.ai/t/assertion-error-in-get-disc-loss/239109/5 "2022-12-29T04:16:07Z")

</div>

Thanks for the prompt response, @paulinpaloalto! The explanation makes complete sense - however, even when I try detaching the fake images before I feed them to the discriminator, I still see the same error at runtime. What might I be doing wrong?

```
fake = gen(noise).detach()
pred_fake = disc(fake)

```

---

<div class="post-metadata">

**Author:** ![paulinpaloalto](https://sea1.discourse-cdn.com/dlai/user_avatar/community.deeplearning.ai/paulinpaloalto/32/187_2.png) [@paulinpaloalto](https://community.deeplearning.ai/u/paulinpaloalto)\
**Post date:** [December 29, 2022, 4:55am UTC](https://community.deeplearning.ai/t/assertion-error-in-get-disc-loss/239109/6 "2022-12-29T04:55:06Z")

</div>

Well, how about trying to detach the fake images _after_ the generator and _before_ you feed them to the discriminator? That’s how I implemented it and it worked for me.

But I would have expected your solution not to throw that particular error. The other thing to keep in mind that just typing new code into a function and then calling it again does nothing: it still runs the old code. You have to click “Shift-Enter” on the actual function cell to execute the new code and get it generated into the runtime image.

---

<div class="post-metadata">

**Author:** ![snc](https://avatars.discourse-cdn.com/v4/letter/s/c77e96/32.png) [@snc](https://community.deeplearning.ai/u/snc)\
**Post date:** [December 29, 2022, 5:10am UTC](https://community.deeplearning.ai/t/assertion-error-in-get-disc-loss/239109/7 "2022-12-29T05:10:20Z")

</div>

It might be something else, in that case. I’ll keep trying. Thanks for your help @paulinpaloalto!
