# 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:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sanjayvarma11](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/sanjayvarma11/32/20339_2.png) [@Sanjayvarma11](https://discuss.pytorch.org/u/Sanjayvarma11)\
**Post date:** [February 21, 2020, 8:59am UTC](https://discuss.pytorch.org/t/using-of-data-to-device-in-pytorch/70578/1 "2020-02-21T08:59:07Z")

</div>

Hello guys. I am very interested in learning PyTorch.so I am trying to understand some basic code in PyTorch.  
here are some of the doubts I got while going through the code

1. I found a piece of code like this in training function

```auto
for batch_idx, (data, target) in enumerate(pbar):
    # get samples
    data, target = data.to(device), target.to(device)

```

Here my doubt is what does "data.to(device), target.to(device ) "do in PyTorch.

2)I also found line of code  
optimizer.zero\_grad()  
what does this piece of code do in PyTorch? One of the comments says that it will make gradients zero.  
so what does the advantage if we make gradients to zero.??

---

<div class="post-metadata">

**Author:** ![learner47](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/l/5f9b8f/32.png) [@learner47](https://discuss.pytorch.org/u/learner47)\
**Post date:** [February 21, 2020, 9:23am UTC](https://discuss.pytorch.org/t/using-of-data-to-device-in-pytorch/70578/2 "2020-02-21T09:23:29Z")

</div>

`data.to(device)` moves the data to cpu or GPU based on what `device` is. This is required for faster computations.

In PyTorch, the gradients are accumulated using `loss.backward()` and then the gradients are applied using `optimizer.step()`. The stale gradients from the previous back propagation need to be cleared before running the `optimizer.step()` again. This is achived through `optimzer.zero_grad()`.

---

<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).
