PyTorch - Calculate one step of gradient
Calculate one step of gradient descent for the following tensor:
f(X)=∑loge(xij+3)
X - tensor 3x3
Xt=0 = [[1,2,3],[5,6,7],[8,9,0]]
α=5 - gradient coefficient
find Xt=1
Briefly, it is necessary to find values after applying one step towards gradient.
import torch
# create initial tensor with floats
w = torch.tensor( [[1.,2.,3.],[5.,6.,7.],[8.,9.,0.]], requires_grad=True)
# or it is possible to specify type of data directly
#w = torch.tensor( [[1,2,3],[5,6,7],[8,9,0]], requires_grad=True, dtype=torch.float)
a = 5
function = a * torch.log(w + 3.).sum()
# another way to write functions for torch:
#function = a * (w + 3.).log().sum()
function.backward()
print(w-w.grad)
#tensor([[-0.2500, 1.0000, 2.1667],
# [ 4.3750, 5.4444, 6.5000],
# [ 7.5455, 8.5833, -1.6667]], grad_fn=)
Published: 2022-05-04 22:54:23
Updated: 2022-05-05 02:17:38