# Will tensor.to float32 to float16 lose precision or values?

**URL:** <https://discuss.pytorch.org/t/will-tensor-to-float32-to-float16-lose-precision-or-values/145264>\
**Category:** vision\
**Created:** [March 1, 2022, 9:27am UTC](https://discuss.pytorch.org/t/will-tensor-to-float32-to-float16-lose-precision-or-values/145264 "2022-03-01T09:27:31Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![karangoswamikenz](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/karangoswamikenz/32/47122_2.png) [@karangoswamikenz](https://discuss.pytorch.org/u/karangoswamikenz)\
**Post date:** [March 1, 2022, 9:27am UTC](https://discuss.pytorch.org/t/will-tensor-to-float32-to-float16-lose-precision-or-values/145264/1 "2022-03-01T09:27:31Z")

</div>

Sorry if this is an obvious question. I know it will lose precision and accuracy when converting from float32 to float16 but is there a better way than to use tensor.to() function?

I have a 1x3x576x960 tensor that I want to convert from float32 to float16

I used

l`rs_prev_fp16 = lrs_prev.to(torch.float16)`

---

<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 1, 2022, 9:35am UTC](https://discuss.pytorch.org/t/will-tensor-to-float32-to-float16-lose-precision-or-values/145264/2 "2022-03-01T09:35:10Z")

</div>

I’m not sure I understand the question completely, but you are right that transforming a `float32` tensor to `float16` would lose precision.  
The `to()` operation is the standard way of converting a `dtype`. Alternatively, you could also use `a = b.half()` which would have the same effect.

---

<div class="post-metadata">

**Author:** ![Eta\_C](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/eta_c/32/17667_2.png) [@Eta\_C](https://discuss.pytorch.org/u/Eta_C)\
**Post date:** [March 2, 2022, 7:45am UTC](https://discuss.pytorch.org/t/will-tensor-to-float32-to-float16-lose-precision-or-values/145264/3 "2022-03-02T07:45:29Z")

</div>

About the accuracy, if your network architecture is `backbone + classification`, there may be a better choice that apply f16 only in `backbone` while training.

PS: sometimes, f16 may lead to `NaN` loss if there are BN layers in your network.
