# Variable type convert in the forward

**URL:** <https://discuss.pytorch.org/t/variable-type-convert-in-the-forward/34089>\
**Category:** Uncategorized\
**Created:** [January 8, 2019, 7:06am UTC](https://discuss.pytorch.org/t/variable-type-convert-in-the-forward/34089 "2019-01-08T07:06:29Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Danfeng\_Zhang](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/danfeng_zhang/32/18097_2.png) [@Danfeng\_Zhang](https://discuss.pytorch.org/u/Danfeng_Zhang)\
**Post date:** [January 8, 2019, 7:06am UTC](https://discuss.pytorch.org/t/variable-type-convert-in-the-forward/34089/1 "2019-01-08T07:06:29Z")

</div>

Hello, I want to convert Variable “FloatTensor” to “LongTensor” in the forward function. I want to know can it work in the backward? Thank you.

---

<div class="post-metadata">

**Author:** ![kris-singh](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/kris-singh/32/4580_2.png) [@kris-singh](https://discuss.pytorch.org/u/kris-singh)\
**Post date:** [January 8, 2019, 10:15am UTC](https://discuss.pytorch.org/t/variable-type-convert-in-the-forward/34089/2 "2019-01-08T10:15:30Z")

</div>

No this won’t work.  
A quick check

```auto
 a = torch.ones(10, requires_grad=True) # Works
 a = torch.ones(10, requires_grad=True, dtype=torch.long) # Error (Only Tensors of floating point can require gradient)

```

---

<div class="post-metadata">

**Author:** ![JuanFMontesinos](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/juanfmontesinos/32/76115_2.png) [@JuanFMontesinos](https://discuss.pytorch.org/u/JuanFMontesinos)\
**Post date:** [January 8, 2019, 12:05pm UTC](https://discuss.pytorch.org/t/variable-type-convert-in-the-forward/34089/3 "2019-01-08T12:05:36Z")

</div>

In addition, casting is a type of quantization, which is not differentiable.

---

<div class="post-metadata">

**Author:** ![Danfeng\_Zhang](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/danfeng_zhang/32/18097_2.png) [@Danfeng\_Zhang](https://discuss.pytorch.org/u/Danfeng_Zhang)\
**Post date:** [January 8, 2019, 12:33pm UTC](https://discuss.pytorch.org/t/variable-type-convert-in-the-forward/34089/4 "2019-01-08T12:33:21Z")

</div>

Ok,Thanks too much, but I have another question. when one require\_grad of Parameter is false, layers before it will not backpropogate? Is it right? I am confused about the meaning of require\_grad. Could You help me explain it?
