DEV Community

Super Kai (Kazuya Ito)
Super Kai (Kazuya Ito)

Posted on • Edited on

Set `requires_grad` and get `grad` in PyTorch

Buy Me a Coffee

*Memos:

You can set requires_grad and get grad as shown below:

*Memos:

tensor(). *My post explains tensor():

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.))
Enter fullscreen mode Exit fullscreen mode

arange(). *My post explains arange():

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.]))
Enter fullscreen mode Exit fullscreen mode

rand(). *My post explains rand():

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.]))
Enter fullscreen mode Exit fullscreen mode

rand_like(). *My post explains rand_like():

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.]))
Enter fullscreen mode Exit fullscreen mode

zeros(). *My post explains zeros():

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.]))
Enter fullscreen mode Exit fullscreen mode

zeros_like(). *My post explains zeros_like():

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.]))
Enter fullscreen mode Exit fullscreen mode

full(). *My post explains full():

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.]))
Enter fullscreen mode Exit fullscreen mode

full_like(). *My post explains full_like():

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.]))
Enter fullscreen mode Exit fullscreen mode

eye(). *My post explains eye():

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.]]))
Enter fullscreen mode Exit fullscreen mode

Top comments (0)