*Memos:
- My post explains requires_grad.
- My post explains how to set and get dtype.
- My post explains how to set and get device.
-
My post explains how to set
keepdim
argument. -
My post explains how to set
out
argument.
You can set requires_grad and get grad as shown below:
*Memos:
- I selected some popular
requires_grad
argument functions such as tensor(), arange(), rand(), rand_like(), zeros(), zeros_like(), full(), full_like() and eye(). - Basically,
requires_grad
(Optional-Default:False
-Type:bool
). - Basically,
requires_grad=
must be used. -
My post explains
requires_grad
and backward() withtensor()
.
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.]]))
Top comments (0)