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

Tensor 5 functions

Assignment 1 - Working example of 5 PyTorch functions

PyTorch is an open-source machine learning library for Python. It is primarily developed by Facebook's artificial-intelligence research group, and Uber's "Pyro" Probabilistic programming language software is built on it.

# Import torch and other required modules
import torch

Function 1 - torch.linspace()

Returns a one-dimensional tensor of steps equally spaced points between start and end.
Linspace takes 3 main arguments start, end & steps
If steps not given, the default value for steps 100 will be assigned.

Linspace calculates the difference between start & end and divide it by nummber of steps -1 and then adds this difference to form the consecutive nnumbers

x=torch.linspace(8,9,5)
print(x)
x.numel()
tensor([8.0000, 8.2500, 8.5000, 8.7500, 9.0000])
5

Start = 8
Stop = 9
Steps = 5
so the difference is calculated as 9-8/(5-1) = 1/4 = 0.25, which is added to start to form consecutive equally spaced numbers