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

Image of Docusign

🛠️ Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more

Top comments (0)

Speedy emails, satisfied customers

Postmark Image

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

👋 Kindness is contagious

Dive into an ocean of knowledge with this thought-provoking post, revered deeply within the supportive DEV Community. Developers of all levels are welcome to join and enhance our collective intelligence.

Saying a simple "thank you" can brighten someone's day. Share your gratitude in the comments below!

On DEV, sharing ideas eases our path and fortifies our community connections. Found this helpful? Sending a quick thanks to the author can be profoundly valued.

Okay