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

Week 1 Assignement

Use 5 different functions about tensors

PyTorch is a librairy focused on tensor manipulation, it is one of the preferred librairy to work with machine learning models (alongside TensorFlow).

As we start our journey with PyTorch I relied on my linear algebra memories to pick functions that, I think, will have interest in manipulating datasets or creating tensors for our models to try. So I chose to focused on the 5 following functions:

  • torch.ones
  • torch.eye
  • torch.randn
  • torch.where
  • torch.cat
# Import torch and other required modules
import torch
import numpy

Function 1 - torch.ones

Torch.ones is a method allowing the creation of a tensor of the defined size filled with floating 1.

# The syntax is as follows:
# torch.ones(*sizes, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False)
# all parameters except *sizes are optional
# Example 1 - creation of 1x3 tensor
torch.ones(3)
tensor([1., 1., 1.])