# Using of data.to(device ) in pytorch

**URL:** <https://discuss.pytorch.org/t/using-of-data-to-device-in-pytorch/70578>\
**Category:** vision\
**Created:** [February 21, 2020, 8:59am UTC](https://discuss.pytorch.org/t/using-of-data-to-device-in-pytorch/70578 "2020-02-21T08:59:07Z")\
**Posts on this page:** 1\
**Showing post:** 3

<div class="post-metadata">

**Author:** ![mmisiur](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/mmisiur/32/10162_2.png) [@mmisiur](https://discuss.pytorch.org/u/mmisiur)\
**Post date:** [February 21, 2020, 9:25am UTC](https://discuss.pytorch.org/t/using-of-data-to-device-in-pytorch/70578/3 "2020-02-21T09:25:38Z")

</div>

`.to(x)` as states in [Docs](https://pytorch.org/docs/stable/tensors.html#torch.Tensor.to) moves the tensor from current device to the device `x`, i.e. cpu (`"cpu"`) or gpu (`"cuda:0"`, `"cuda:1"` …). It’s useful, because you can specify `x` in the beginning of code and from there do not care whether it is cpu or gpu, and just move all your tensors, models etc. to it. The alternative is to call `.cpu()` or `cuda()`, but it lacks the flexibility of `.to()`. There is awesome explanation of this in the official PyTorch Docs [here](https://pytorch.org/docs/stable/notes/cuda.html).

about `.zero_grad()` you can find extensive discussion [here](https://discuss.pytorch.org/t/why-do-we-need-to-set-the-gradients-manually-to-zero-in-pytorch/4903).

---

_[View the full topic](https://discuss.pytorch.org/t/using-of-data-to-device-in-pytorch/70578)._
