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

PyTorch Introductory - Assignment 1

A brief walkthrough with Shripal ...

... to understand 5 of the functions that can be applied to tensors - torch.Tensor objects.

A short introduction about PyTorch and about the chosen functions.

  • function 1 - new_empty()
  • function 2 - add()
  • function 3 - sqrt()
  • function 4 - bernoulli()
  • function 5 - sin()
# Import torch and other required modules
import torch

Function 1 - torch.Tensor.new_empty()

  • returns a new empty Tensor, with specified size (from arguments) with uninitialized elements and attributes (torch.dtype and torch.device) same as that of the mentioned tensor.
# Example 1 - working

t1 = torch.zeros(())
print("t1 =", t1, "\nt1.dtype = ", t1.dtype, ";   t1.device = ", t1.device)
print()

t2 = t1.new_empty((2,4))
print("t1 =", t1)
print("t2 =", t2, "\nt2.dtype = ", t2.dtype, ";   t2.device = ", t2.device)
t1 = tensor(0.) t1.dtype = torch.float32 ; t1.device = cpu t1 = tensor(0.) t2 = tensor([[7.5510e-34, 4.5623e-41, 7.5510e-34, 4.5623e-41], [4.4842e-44, 0.0000e+00, 8.9683e-44, 0.0000e+00]]) t2.dtype = torch.float32 ; t2.device = cpu
Explanation about example:

By default, the dtype of t1 is torch.float32
so, dtype of t2 is also torch.float32 since it is not explicitly mentioned to something else.

Same is for device = cpu