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

Pytorch Functions

Five Pytorch Examples

An short introduction about PyTorch and about the chosen functions.

Pytorch

It’s a Python-based scientific computing module mainly used for the following:

1.To be used in place of NumPy to get the benifits of the power of GPUs
2.A deep learning research platform that provides maximum flexibility and speed

The following are the examples of the 5 pytorch functions

  • torch.zeros : creates an tensor(out of matrix) with all values 0

  • torch.view : re-sizes the tensor to specific dimension

  • torch.add : adds two tensors

  • torch.copy_ : copies one tensor to the other

  • torch.to : to move the tensor to a specific device e.g "cuda" or "cpu"

# Import torch and other required modules
import torch

Function 1 - torch.zeros

Creates a tensor with all values 0

# Example 1 - create an zero 5*3 tensor with values are 0
x = torch.zeros(5, 3, dtype=torch.long)
print(x)
tensor([[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]])

The above created a torch tensor of 5 * 3 with all values are 0