Learn practical skills, build real-world projects, and advance your career

5 of tensor functions

Adhy Riz

An short introduction about PyTorch and about the chosen functions.

  • backward
  • resize
  • repeat
  • tolist
  • clone
# Import torch and other required modules
import torch

Function 1 - backward

Computes the gradient of current tensor w.r.t. graph leaves.

# Example 1
t = torch.tensor([1,2,3],dtype=torch.float32, requires_grad=True)
u = t ** 2
v = (u*2).sum()
v.backward()  
print(t.grad)
tensor([ 4., 8., 12.])

v counting the gradient using backward