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

Pytorch

5 functions in Pytorch with examples

Pytorch is library for python with a lot of useful in-built functions to make deep learning tasks easier.

  • new_ones()
  • torch.inverse()
  • fill_diagonal_()
  • new_full()
  • abs()
# Import torch and other required modules
import torch

new_ones()

It is used to create a tensor of mentioned size filled with ones

# Example 1 
x = torch.tensor(4.)
x = x.new_ones((2,3))
x
tensor([[1., 1., 1.],
        [1., 1., 1.]])

I have created a tensor named 'x' and assigned its data type is floating point number since i have mentioned as '4.'. With the help of 'new_ones' function I have filled the tensor with ones.

Note:Similarly we can use 'new_zeros' function to fill tensors with zeros