Set requires_grad with requires_grad argument functions and get it in PyTorch
Tags: pytorch, gradient, grad, function
Published: 2024-06-27 16:34:53

You can set `requires_grad` with the functions which have `requires_grad` argument and get it with [grad](https://pytorch.org/docs/stable/generated/torch.Tensor.grad.html) as shown below:

*Memos:
- I selected some popular `requires_grad` argument functions such as [tensor()](https://pytorch.org/docs/stable/generated/torch.tensor.html), [arange()](https://pytorch.org/docs/stable/generated/torch.arange.html), [rand()](https://pytorch.org/docs/stable/generated/torch.rand.html), [rand_like()](https://pytorch.org/docs/stable/generated/torch.rand_like.html), [zeros()](https://pytorch.org/docs/stable/generated/torch.zeros.html), [zeros_like()](https://pytorch.org/docs/stable/generated/torch.zeros_like.html), [full()](https://pytorch.org/docs/stable/generated/torch.full.html), [full_like()](https://pytorch.org/docs/stable/generated/torch.full_like.html) and [eye()](https://pytorch.org/docs/stable/generated/torch.eye.html).
- `requires_grad`(Optional-Default:`False`-Type:`bool`).
- Basically, `requires_grad=` is needed.
- [My post](https://dev.to/hyperkai/requiresgradtrue-with-a-tensor-backward-and-retaingrad-in-pytorch-4kf7) explains `requires_grad` and [backward()](https://pytorch.org/docs/stable/generated/torch.Tensor.backward.html) with `tensor()`.

`tensor()`. *[My post](https://dev.to/hyperkai/create-a-tensor-in-pytorch-127g) explains `tensor()`:

```python
import torch

my_tensor = torch.tensor(data=7., requires_grad=True)

my_tensor, my_tensor.grad
# (tensor(7., requires_grad=True), None)

my_tensor.backward()

my_tensor, my_tensor.grad
# (tensor(7., requires_grad=True), tensor(1.))
```

`arange()`. *[My post](https://dev.to/hyperkai/arange-linspace-logspace-and-normal-in-pytorch-a87) explains `arange()`:

```python
import torch

my_tensor = torch.arange(start=5, end=15, step=3, requires_grad=True)

my_tensor, my_tensor.grad
# (tensor([7.], requires_grad=True), None)

my_tensor.backward()

my_tensor, my_tensor.grad
# (tensor([7.], requires_grad=True), tensor([1.]))
```

`rand()`. *[My post](https://dev.to/hyperkai/rand-randlike-randn-randnlike-randint-and-randperm-in-pytorch-31nc) explains `rand()`:

```python
import torch

my_tensor = torch.rand(size=(1,), requires_grad=True)

my_tensor, my_tensor.grad
# (tensor([0.0030], requires_grad=True), None)

my_tensor.backward()

my_tensor, my_tensor.grad
# (tensor([0.0913], requires_grad=True), tensor([1.]))
```

`rand_like()`. *[My post](https://dev.to/hyperkai/rand-randlike-randn-randnlike-randint-and-randperm-in-pytorch-31nc) explains `rand_like()`:

```python
import torch

my_tensor = torch.rand_like(input=torch.tensor([7.]), 
                            requires_grad=True)
my_tensor, my_tensor.grad
# (tensor([0.4687], requires_grad=True), None)

my_tensor.backward()

my_tensor, my_tensor.grad
# (tensor([0.4687], requires_grad=True), tensor([1.]))
```

`zeros()`. *[My post](https://dev.to/hyperkai/zeros-zeroslike-ones-and-oneslike-in-pytorch-26jm) explains `zeros()`:

```python
import torch

my_tensor = torch.zeros(size=(1,), requires_grad=True)

my_tensor, my_tensor.grad
# (tensor([0.], requires_grad=True), None)

my_tensor.backward()

my_tensor, my_tensor.grad
# (tensor([0.], requires_grad=True), tensor([1.]))
```

`zeros_like()`. *[My post](https://dev.to/hyperkai/zeros-zeroslike-ones-and-oneslike-in-pytorch-26jm) explains `zeros_like()`:

```python
import torch

my_tensor = torch.zeros_like(input=torch.tensor([7.]), 
                             requires_grad=True)
my_tensor, my_tensor.grad
# (tensor([0.], requires_grad=True), None)

my_tensor.backward()

my_tensor, my_tensor.grad
# (tensor([0.], requires_grad=True), tensor([1.]))
```

`full()`. *[My post](https://dev.to/hyperkai/full-and-fulllike-in-pytorch-a8f) explains `full()`:

```python
import torch

my_tensor = torch.full(size=(1,), fill_value=5., requires_grad=True)

my_tensor, my_tensor.grad
# (tensor([5.], requires_grad=True), None)

my_tensor.backward()

my_tensor, my_tensor.grad
# (tensor([5.], requires_grad=True), tensor([1.]))
```

`full_like()`. *[My post](https://dev.to/hyperkai/full-and-fulllike-in-pytorch-a8f) explains `full_like()`:

```python
import torch

my_tensor = torch.full_like(input=torch.tensor([7.]),
                            fill_value=5., 
                            requires_grad=True)
my_tensor, my_tensor.grad
# (tensor([5.], requires_grad=True), None)

my_tensor.backward()

my_tensor, my_tensor.grad
# (tensor([5.], requires_grad=True), tensor([1.]))
```

`eye()`. *[My post](https://dev.to/hyperkai/eye-in-pytorch-1mjj) explains `eye()`:

```python
import torch

my_tensor = torch.eye(n=1, requires_grad=True)

my_tensor, my_tensor.grad
# (tensor([[1.]], requires_grad=True), None)

my_tensor.backward()

my_tensor, my_tensor.grad
# (tensor([[1.]], requires_grad=True), tensor([[1.]]))
```