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

Assignment - 1(Introduction to Tensors in Pytorch and some of their interesting function)

Pytorch is an open source machine learning library based on the torch package. It was primarily developed by FaceBook's AI research team. They provide with end to end research framework. In addition to the framework they allow chaining of high-level nueral network modules since it supports Keras like API in its torch.nn (help in creating and training of the neural network) package. Pytorch is mainly employed for application such as the Computer Vision (use of image processing algorithms to gain high-level understanding from digital images or videos) and Natural Language Processing (ability of a computer program to understand human language).

  • new_full(size, fill_value, dtype=None, device=None, requires_grad=False)
  • torch.ne(input, other, out=None) → Tensor
  • torch.cat()
  • gather()
  • torch.flatten()
# Import torch and other required modules
import torch
import numpy as np

Function 1 -new_full(size, fill_value, dtype=None, device=None, requires_grad=False)

This function is used to return a tensor with a specified size and is filled with a specific value given as input. The output tensor will have the default datatype which can be also be manipulated.

The parameters of the function are :-

(i) device (torch.device, optional) – the desired device of returned tensor. By default the value is set as if None, same torch.device as this tensor.

(ii) requires_grad (bool, optional) – If autograd should record operations on the returned tensor. By default the value is set as False.

(iii) fill_value (scalar) – the number to fill the output tensor with.

(iv) dtype (torch.dtype, optional) – the desired type of returned tensor. By default the value is if None, same torch.dtype as this tensor.

# Example 1 - working
t=torch.zeros([3, 3],dtype=torch.float64)
print(t)
t.new_full([2,4],0.112)
tensor([[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]], dtype=torch.float64)
tensor([[0.1120, 0.1120, 0.1120, 0.1120],
        [0.1120, 0.1120, 0.1120, 0.1120]], dtype=torch.float64)

In above example we can see that first we created a tensor which has three columns and rows and is filled with zeros using the function torch.zeros() it is also specified that it has the datatype torch.float64. Now we use this tensor to create a new one with a different size and is filled its elements with a scalar quantity.