


w = torch.ones(1, requires_grad=True)
w.requires_grad
True
add to the previous result that has require_grad=False
total = w + z
the total sum now requires grad!
total.requires_grad
True
autograd can compute the gradients as well
total.backward()
w.grad
tensor([ 1.])
and no computation is wasted to compute gradients for x, y and z, which don't require grad
z.grad == x.grad == y.grad == None
True




#### Manipulating `requires_grad` flag

Other than directly setting the attribute, you can change this flag `in-place` using [`my_tensor.requires_grad_()`](https://pytorch.org/docs/0.4.0/tensors.html#torch.Tensor.requires_grad_), or, as in the above example, at creation time by passing it in as an argument (default is `False`), e.g.,

```python
>>> existing_tensor.requires_grad_()
>>> existing_tensor.requires_grad
True
>>> my_tensor = torch.zeros(3, 4, requires_grad=True)
>>> my_tensor.requires_grad
True

What about .data?