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

Assignment 1 - All About torch.Tensor

The objective of this assignment is to develop a solid understanding of PyTorch tensors

According to Pytorch's website, Pytorch is a Python-based scientific computing package targeted at two sets of audiences:

  • A replacement for NumPy to use the power of GPUs
  • a deep learning research platform that provides maximum flexibility and speed

Tensors are similar to NumPy's ndarrays, with the addition beign taht Tensors can also be used on a GPU to accelerate computing. A torch.Tensor is a multi-dimensional matrix containing elements of a single data type. Default data type is 32-bit floating point (torch.FloatTensor) and torch.Tensor is an alias for the default.

In this notebook we will look at 5 Tensor functions:

  • torch.empty
  • torch.rand
  • torch.zeros
  • torch.from_numpy
  • torch.max

Imports

# Import torch and other required modules
import torch
import numpy as np

Function 1 - torch.empty

Construct a matrix of given shape, uninitialized

# Example 1 - construct a 5x3 matrix, uninitialized
x = torch.empty(5, 3)
print(x)
tensor([[0.0000e+00, 0.0000e+00, 1.5488e-29], [3.0966e-41, 0.0000e+00, 0.0000e+00], [7.1746e-43, 0.0000e+00, 0.0000e+00], [0.0000e+00, 1.7937e-43, 0.0000e+00], [2.6992e-29, 3.0966e-41, 0.0000e+00]])