Set `requires_grad` and get `grad` in PyTorch

Buy Me a Coffee☕

*Memos:

My post explains requires_grad.

My post explains how to set and get dtype.

My post explains how to set device with device argument functions and get it.

My post explains how to set keepdim with keepdim argument functions…


This content originally appeared on DEV Community and was authored by Super Kai (Kazuya Ito)

Buy Me a Coffee

*Memos:

  • My post explains requires_grad.
  • My post explains how to set and get dtype.
  • My post explains how to set device with device argument functions and get it.
  • My post explains how to set keepdim with keepdim argument functions.
  • My post explains how to set out with out argument functions.

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

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

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

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

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

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

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

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

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


This content originally appeared on DEV Community and was authored by Super Kai (Kazuya Ito)


Print Share Comment Cite Upload Translate Updates
APA

Super Kai (Kazuya Ito) | Sciencx (2024-09-19T16:40:34+00:00) Set `requires_grad` and get `grad` in PyTorch. Retrieved from https://www.scien.cx/2024/09/19/set-requires_grad-and-get-grad-in-pytorch/

MLA
" » Set `requires_grad` and get `grad` in PyTorch." Super Kai (Kazuya Ito) | Sciencx - Thursday September 19, 2024, https://www.scien.cx/2024/09/19/set-requires_grad-and-get-grad-in-pytorch/
HARVARD
Super Kai (Kazuya Ito) | Sciencx Thursday September 19, 2024 » Set `requires_grad` and get `grad` in PyTorch., viewed ,<https://www.scien.cx/2024/09/19/set-requires_grad-and-get-grad-in-pytorch/>
VANCOUVER
Super Kai (Kazuya Ito) | Sciencx - » Set `requires_grad` and get `grad` in PyTorch. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/09/19/set-requires_grad-and-get-grad-in-pytorch/
CHICAGO
" » Set `requires_grad` and get `grad` in PyTorch." Super Kai (Kazuya Ito) | Sciencx - Accessed . https://www.scien.cx/2024/09/19/set-requires_grad-and-get-grad-in-pytorch/
IEEE
" » Set `requires_grad` and get `grad` in PyTorch." Super Kai (Kazuya Ito) | Sciencx [Online]. Available: https://www.scien.cx/2024/09/19/set-requires_grad-and-get-grad-in-pytorch/. [Accessed: ]
rf:citation
» Set `requires_grad` and get `grad` in PyTorch | Super Kai (Kazuya Ito) | Sciencx | https://www.scien.cx/2024/09/19/set-requires_grad-and-get-grad-in-pytorch/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.