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

5 Useful Pytorch Functions

In this post we are going to look into the following interesting functions which are readily available in pytorch for faster prototyping and development of the Deep Learning Project.

  • torch.addcmul()
  • torch.gt()
  • torch.flatten()
  • torch.clamp()
  • torch.cat()
# Import torch and other required modules
import torch

Function 1 - torch.addcmul()

torch.addcmul function helps us in element wise multiplication of the passed in tensors (tensor 1 and tensor 2) take a fraction of it by multiplying with a constant value and summing up the resultant value with the input tensor.

The function can be represented as a formula as follows.

outi=inputi+value×tensor1i×tensor2i\text{out}_i = \text{input}_i + \text{value} \times \text{tensor1}_i \times \text{tensor2}_i

Function takes in input, value, tensor1 and tensor2 as inputs, creating a tensor output (outiout_i).

Lets look at the examples

Example 1

# Input Tensor
input_t = torch.eye(1,2)

#Value
value=0.4

#Tensor 1 & 2
tensor1 = torch.eye(1,2)
tensor2 = torch.eye(2,1)

# Function
torch.addcmul(input_t,value,tensor1,tensor2)
tensor([[1.4000, 0.0000],
        [1.0000, 0.0000]])