Some commonly used PyTorch functions

PyTorch is an open source python library used for machine learning application.

  • one_hot: a one-hot is an encoding of bits with default on value 1 and off value 0.
  • cosine_similarity: a measure of similarity between two non-zero vectors of an inner product space.
  • softmax: normalize the output of a network to probability distribution over the predicted output class.
  • relu: a linear function that will output the input directly if it is positive, otherwise, it will output zero.
  • dropout: a regularization method to reduce overfitting in neural networks.
# Import torch and other required modules
import torch
import torch.nn.functional as F

Function 1 - torch.nn.functional.one_hot

SYNTAX: torch.nn.functiona.one_hot(tensor, num_classes)

This function take one tensor of shape () and returns a tensor of shape (, num_classes) that have zeros everywhere except the index value of the input tensor which is 1.

# Example 1 - working
t1 = torch.tensor([0, 1, 2])
print('Original tensor')
print(t1,'\n')

print('One hot tensor')
F.one_hot(t1)
Original tensor tensor([0, 1, 2]) One hot tensor
tensor([[1, 0, 0],
        [0, 1, 0],
        [0, 0, 1]])

Explanation about example 1:

Here we can see the output of the one_hot operation on the input tensor [0, 1, 2]. The output is a tensor of shape (3, 3) with zeros in all the places except for the corresponding indices of the input tensor which has a value of 1.