# RuntimeError: element 0 of variables does not require grad and does not have a grad\_fn

**URL:** <https://discuss.pytorch.org/t/runtimeerror-element-0-of-variables-does-not-require-grad-and-does-not-have-a-grad-fn/11074>\
**Category:** autograd\
**Created:** [December 12, 2017, 6:18pm UTC](https://discuss.pytorch.org/t/runtimeerror-element-0-of-variables-does-not-require-grad-and-does-not-have-a-grad-fn/11074 "2017-12-12T18:18:32Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Zhang\_Wen](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/zhang_wen/32/794_2.png) [@Zhang\_Wen](https://discuss.pytorch.org/u/Zhang_Wen)\
**Post date:** [December 12, 2017, 6:18pm UTC](https://discuss.pytorch.org/t/runtimeerror-element-0-of-variables-does-not-require-grad-and-does-not-have-a-grad-fn/11074/1 "2017-12-12T18:18:32Z")

</div>

hi, i have a problem here, i got a sequence of Variables which are the outputs of the bi-directional RNN, and i stacked them into a matrix `xs_h` whose dimension is `(seq_length, batch_size, hidden_size)`, them i want to update the matrix `xs_h` by convoluting on two slices in `xs_h`, some codes are as follows:

`new_xs_h = xs_h.clone()`  
`vp, vc = xs_h[idx_0, bidx], xs_h[idx_1, bidx]`  
`x = tc.stack([self.f1(vp), self.f2(vc)], dim=1)[None, :, :]`  
`new_xs_h[idx_1, bidx] = self.tanh(self.l_f2(self.conv(x).squeeze()))`

actually, i want to update the Variable `xs_h` and then let the new updated matrix `new_xs_h` get into my computation graph again. However, i got following errors when i call `backward()` after the running of above code:

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

i do not kown why, any reply will be appreciated.  
thanks.

---

<div class="post-metadata">

**Author:** ![richard](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/richard/32/1861_2.png) [@richard](https://discuss.pytorch.org/u/richard)\
**Post date:** [December 12, 2017, 6:30pm UTC](https://discuss.pytorch.org/t/runtimeerror-element-0-of-variables-does-not-require-grad-and-does-not-have-a-grad-fn/11074/2 "2017-12-12T18:30:49Z")

</div>

It sounds like the problem is that your `xs_h` don’t have `requires_grad=True`. Have you tried creating Variables with `requires_grad=True`?

---

<div class="post-metadata">

**Author:** ![Zhang\_Wen](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/zhang_wen/32/794_2.png) [@Zhang\_Wen](https://discuss.pytorch.org/u/Zhang_Wen)\
**Post date:** [December 12, 2017, 6:37pm UTC](https://discuss.pytorch.org/t/runtimeerror-element-0-of-variables-does-not-require-grad-and-does-not-have-a-grad-fn/11074/4 "2017-12-12T18:37:40Z")

</div>

thanks for reply, the Variable xs\_h is not created by myself, it is the output of the Bi-RNN by feeding the embedding of the words. so the requires\_grad attribute is False.

---

<div class="post-metadata">

**Author:** ![richard](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/richard/32/1861_2.png) [@richard](https://discuss.pytorch.org/u/richard)\
**Post date:** [December 12, 2017, 7:03pm UTC](https://discuss.pytorch.org/t/runtimeerror-element-0-of-variables-does-not-require-grad-and-does-not-have-a-grad-fn/11074/5 "2017-12-12T19:03:23Z")

</div>

Okay. You can make a new Variable with requires\_grad = True:

```auto
var_xs_h = Variable(xs_h.data, requires_grad=True)

```

---

<div class="post-metadata">

**Author:** ![Brando\_Miranda](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/brando_miranda/32/14355_2.png) [@Brando\_Miranda](https://discuss.pytorch.org/u/Brando_Miranda)\
**Post date:** [May 8, 2018, 12:45am UTC](https://discuss.pytorch.org/t/runtimeerror-element-0-of-variables-does-not-require-grad-and-does-not-have-a-grad-fn/11074/6 "2018-05-08T00:45:57Z")

</div>

Did the suggestion solve your problem? I have the same error thrown at me but the error isn’t very helpful since I don’t know what of my code has the `require_gradients` set to false. I went ahead and set everything I could find to trainable but it still didn’t fix it…

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

---

<div class="post-metadata">

**Author:** ![gt\_tugsuu](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/gt_tugsuu/32/11678_2.png) [@gt\_tugsuu](https://discuss.pytorch.org/u/gt_tugsuu)\
**Post date:** [July 11, 2018, 4:01am UTC](https://discuss.pytorch.org/t/runtimeerror-element-0-of-variables-does-not-require-grad-and-does-not-have-a-grad-fn/11074/7 "2018-07-11T04:01:42Z")

</div>

For me ‘loss = Variable(loss, requires\_grad = True)’ worked.

I was trying to use float type of loss, and it was giving me the same error.

---

<div class="post-metadata">

**Author:** ![mnazaal](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/mnazaal/32/12026_2.png) [@mnazaal](https://discuss.pytorch.org/u/mnazaal)\
**Post date:** [July 26, 2018, 3:05pm UTC](https://discuss.pytorch.org/t/runtimeerror-element-0-of-variables-does-not-require-grad-and-does-not-have-a-grad-fn/11074/8 "2018-07-26T15:05:25Z")

</div>

Got the same error in a very different problem and gt\_tugsuu 's post helped. thanks

---

<div class="post-metadata">

**Author:** ![tomorrow](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/t/b3f665/32.png) [@tomorrow](https://discuss.pytorch.org/u/tomorrow)\
**Post date:** [September 20, 2018, 7:44am UTC](https://discuss.pytorch.org/t/runtimeerror-element-0-of-variables-does-not-require-grad-and-does-not-have-a-grad-fn/11074/9 "2018-09-20T07:44:26Z")

</div>

Thank you for your answer

---

<div class="post-metadata">

**Author:** ![sonal\_garg](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/sonal_garg/32/12513_2.png) [@sonal\_garg](https://discuss.pytorch.org/u/sonal_garg)\
**Post date:** [July 8, 2019, 4:20am UTC](https://discuss.pytorch.org/t/runtimeerror-element-0-of-variables-does-not-require-grad-and-does-not-have-a-grad-fn/11074/12 "2019-07-08T04:20:01Z")

</div>

Hi richard, I was also getting same error using this it got fixed but now loss is not decreasing with every epochs . It is constant. I think this is related to this variable.

---

<div class="post-metadata">

**Author:** ![Mylinda](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/m/c57346/32.png) [@Mylinda](https://discuss.pytorch.org/u/Mylinda)\
**Post date:** [July 16, 2019, 1:57am UTC](https://discuss.pytorch.org/t/runtimeerror-element-0-of-variables-does-not-require-grad-and-does-not-have-a-grad-fn/11074/13 "2019-07-16T01:57:05Z")

</div>

Could you give the link of gt\_tugsuu’s post? I meet the problem of getting the same output and loss. Thanks

---

<div class="post-metadata">

**Author:** ![Mylinda](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/m/c57346/32.png) [@Mylinda](https://discuss.pytorch.org/u/Mylinda)\
**Post date:** [July 16, 2019, 4:00am UTC](https://discuss.pytorch.org/t/runtimeerror-element-0-of-variables-does-not-require-grad-and-does-not-have-a-grad-fn/11074/14 "2019-07-16T04:00:34Z")

</div>

Seems my problem is not caused by the same error, so I do not need the post. Thanks!

---

<div class="post-metadata">

**Author:** ![yGalindo](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/y/c68b51/32.png) [@yGalindo](https://discuss.pytorch.org/u/yGalindo)\
**Post date:** [October 31, 2019, 10:57pm UTC](https://discuss.pytorch.org/t/runtimeerror-element-0-of-variables-does-not-require-grad-and-does-not-have-a-grad-fn/11074/16 "2019-10-31T22:57:32Z")

</div>

@sonal_garg and other people that may be tempted to use loss.requires\_grad=True or some similar fix, note that if you are even able to set the requires\_grad of your loss, something is very wrong.

You can only set requires\_grad of leaf nodes of the computation graph, that is, tensors that do not further propagate the gradient (see [this explanation](https://towardsdatascience.com/pytorch-autograd-understanding-the-heart-of-pytorchs-magic-2686cd94ec95)). If your loss does not propagate the gradient to the rest of your model, you are not training the model, and the loss is useless (even if you force it to require a gradient).

Chances are, some layer is set as requires\_grad=False somewhere in the code (probably all of them or the last layer), or the computation graph is detached. Possibly the backward call is wrapped in a with torch.no\_grad().

---

<div class="post-metadata">

**Author:** ![RylanSchaeffer](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/rylanschaeffer/32/17130_2.png) [@RylanSchaeffer](https://discuss.pytorch.org/u/RylanSchaeffer)\
**Post date:** [January 4, 2020, 3:31pm UTC](https://discuss.pytorch.org/t/runtimeerror-element-0-of-variables-does-not-require-grad-and-does-not-have-a-grad-fn/11074/17 "2020-01-04T15:31:49Z")

</div>

I have the same error for different code. @richard@yGalindo , how does one generally locate the source of the error? I’ve already checked that my loss has `requires_grad=True` and that the model’s input to the loss function has `requires_grad=True`.

---

<div class="post-metadata">

**Author:** ![RylanSchaeffer](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/rylanschaeffer/32/17130_2.png) [@RylanSchaeffer](https://discuss.pytorch.org/u/RylanSchaeffer)\
**Post date:** [January 4, 2020, 3:50pm UTC](https://discuss.pytorch.org/t/runtimeerror-element-0-of-variables-does-not-require-grad-and-does-not-have-a-grad-fn/11074/18 "2020-01-04T15:50:30Z")

</div>

I found my problem. For anyone else after me, I had a loop making multiple passes through the model and adding each pass’s loss to a running total. But I initialized the total loss to 0 outside the loop and that tensor had `requires_grad=False`, a property that was preserved as each pass’s loss was added to the total loss. I solved this using `total_reward = torch.zeros(1, requires_grad=True)`.

---

<div class="post-metadata">

**Author:** ![angelinaG](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/angelinag/32/11990_2.png) [@angelinaG](https://discuss.pytorch.org/u/angelinaG)\
**Post date:** [January 13, 2020, 9:08am UTC](https://discuss.pytorch.org/t/runtimeerror-element-0-of-variables-does-not-require-grad-and-does-not-have-a-grad-fn/11074/19 "2020-01-13T09:08:07Z")

</div>

Thank you for sharing this!  
My problem was resolved right away!  
loss.requires\_grad = True (edited the typo)

---

<div class="post-metadata">

**Author:** ![Vijit\_Mehrotra](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/vijit_mehrotra/32/21608_2.png) [@Vijit\_Mehrotra](https://discuss.pytorch.org/u/Vijit_Mehrotra)\
**Post date:** [March 12, 2020, 12:14pm UTC](https://discuss.pytorch.org/t/runtimeerror-element-0-of-variables-does-not-require-grad-and-does-not-have-a-grad-fn/11074/20 "2020-03-12T12:14:13Z")

</div>

Hi Everyone i am getting RuntimeError: element 11 of tensors does not require grad and does not have a grad\_fn. when I am running a GAN Architecture for getting the Gaussian Model and I am using WGANLOSS as well.

How to fix the error so that i can get th required output.

---

<div class="post-metadata">

**Author:** ![ptrblck](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ptrblck/32/1823_2.png) [@ptrblck](https://discuss.pytorch.org/u/ptrblck)\
**Post date:** [March 12, 2020, 11:37pm UTC](https://discuss.pytorch.org/t/runtimeerror-element-0-of-variables-does-not-require-grad-and-does-not-have-a-grad-fn/11074/21 "2020-03-12T23:37:33Z")

</div>

The error is usually thrown, if you detach a tensor from the computation graph, e.g. by:

- calling `tensor.detach()` on it directly
- transforming it to a numpy array or a Python literal
- recreating a tensor via `torch.tensor(tensor)`

Could you check your code for any of these ops?

---

<div class="post-metadata">

**Author:** ![Vijit\_Mehrotra](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/vijit_mehrotra/32/21608_2.png) [@Vijit\_Mehrotra](https://discuss.pytorch.org/u/Vijit_Mehrotra)\
**Post date:** [March 13, 2020, 12:36am UTC](https://discuss.pytorch.org/t/runtimeerror-element-0-of-variables-does-not-require-grad-and-does-not-have-a-grad-fn/11074/23 "2020-03-13T00:36:28Z")

</div>

Actauly I have not callled torch.detach() nor I have trasnformed any tensor to numpy or python literal.

It is like when i am trying to do Higher order differencation of tensors it is giving me error on them.  
For me : HVP\_Y = torch.autograd.grad(inputs =y,outputs =DerivateX\_g ,grad\_outputs=B\_X, retain\_graph =True, allow\_unused = True) is the place where i am getting the error at in my code.

---

<div class="post-metadata">

**Author:** ![ptrblck](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ptrblck/32/1823_2.png) [@ptrblck](https://discuss.pytorch.org/u/ptrblck)\
**Post date:** [March 13, 2020, 3:53am UTC](https://discuss.pytorch.org/t/runtimeerror-element-0-of-variables-does-not-require-grad-and-does-not-have-a-grad-fn/11074/24 "2020-03-13T03:53:09Z")

</div>

Could you print the tensors `y` and `DerivateX_g` please?

---

<div class="post-metadata">

**Author:** ![Vijit\_Mehrotra](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/vijit_mehrotra/32/21608_2.png) [@Vijit\_Mehrotra](https://discuss.pytorch.org/u/Vijit_Mehrotra)\
**Post date:** [March 13, 2020, 6:10am UTC](https://discuss.pytorch.org/t/runtimeerror-element-0-of-variables-does-not-require-grad-and-does-not-have-a-grad-fn/11074/25 "2020-03-13T06:10:13Z")

</div>

I have printed both the Tensor y and DerivateX\_g and put that in image for easy purpose.

 ![Pytorch_Error](https://discuss.pytorch.org/uploads/default/original/3X/c/6/c617ec8933d2dba28390b1c2f707f467bba9fec3.png)

[Next page](https://discuss.pytorch.org/t/runtimeerror-element-0-of-variables-does-not-require-grad-and-does-not-have-a-grad-fn/11074.md?page=2)
