5 Important PyTorch Tensor Functions

Amartya Nambiar
2 min readJun 21, 2021

PyTorch is an open source machine learning library based on the Torch library, used for applications such as computer vision and natural language processing, primarily developed by Facebook’s AI Research lab.

Let’s explore the 5 PyTorch Tensor functions which makes lives of ML and DL enthusiasts like me better!

  • torch.sort
  • torch.imag
  • torch.matmul
  • torch.gcd
  • torch.eye

torch.sort()

This function sorts the elements of the input tensor along a given dimension in ascending or descending order with respect to the parameter set.

Parameters:

  • input (Tensor) — the input tensor.
  • dim (int, optional) — the dimension to sort along
  • stable (bool, optional) — makes the sorting routine stable, which guarantees that the order of equivalent elements is preserved.
  • descending (bool, optional) — when True ,sorts in descending order.

torch.imag()

This function returns a new tensor containing imaginary values of the tensor given as the input(also known as self tensor).

torch.matmul()

It gives the Matrix product of two tensors.

torch.gcd()

Computes the element-wise greatest common divisor (GCD) of input 1 and 2. Both inputs must have integer types.

torch.eye()

It returns a 2-D tensor with ones on the diagonal and zeros elsewhere. This function is very helpful to create identity matrices which is used very often.

Conclusion

In this blog, we explored 5 important PyTorch Tensor functions. All functions were show with two working examples and one example where the function breaks. I hope that you get to use these functions in your upcoming project !

Special thanks to Aakash N S for the wonderful Deep Learning course program on jovian.ai . Writing a blog of the hands-on assignment was an optional supplement but I decided to take it up and guess what I just wrote my first blog. Please let me know where could I improve myself.

Many more blogs coming soon. Till then, GoodBye! Happy Coding!

Reference Links

Provide links to your references and other interesting articles about tensors

Official documentation for tensor operations: https://pytorch.org/docs/stable/torch.html

Wikipedia page for PyTorch: https://en.wikipedia.org/wiki/PyTorch

--

--