Beginners guide to Tensor operations in PyTorch

Harsh R
4 min readMay 29, 2020

--

This is the first post in the series of tutorials on “Deep learning with PyTorch: Zero to GANs” thought by the team at Jovian.ml . The main aim of this post is to provide a beginner friendly introduction on how to perform tensor operations in PyTorch.

This post is targeted to those who are starting out with deep learning using PyTorch.

Cool, now let's begin.

Let’s begin by answering the question What is PyTorch?

PyTorch is an open-source, community-driven deep learning framework developed by Facebook’s artificial intelligence research group. PyTorch is widely used for several deep learning applications such as natural language processing, computer vision applications, image classification, transfer learning, and so on.

What are tensors in PyTorch?

Tensors are n-dimensional matrices. Tensors are core to the PyTorch library and are used for efficient computation in deep learning. A tensor of order zero is a number. A tensor of order one is an array of numbers i.e. a vector. A tensor of order two is an array of vectors, or a matrix. Unlike NumPy arrays, we can use tensors with GPUs as well, giving us the advantage of faster computation.

Tensors as a generalization of multidimensional array. (Source: Book — NLP with PyTorch by Delip Rao)

Following are the datatype of a tensor:

  • IntTensor
  • FloatTensor
  • DoubleTensor
  • LongTensor
  • HalfTensor

PyTorch allows us to create tensors in many ways. I am not going to go deep into it as this is not the aim of this article. Let’s take a look at some of them.

#import all prerequisites import torch#creating a tensor with random values torch.tensor([2, 3])#Creating a tensor with random values from uniform distribution on interval [0, 1)torch.rand(2, 3)#Creating a tensor with random values from standard normal distributiontorch.randn(2, 3)# Creating zero-valued tensor torch.zeros(2, 3)#Creating tensor with onestorch.ones(2, 3)

Now, we know what is PyTorch, tensors. Its time to dive into Tensor Operations.

1. torch.reshape(input, shape) → Tensor

Our first function is reshape(). It was introduced in version 0.4 . It returns a tensor with the same data as input but with a specified shape. When we use torch.reshape(), the new tensor could be a view of the original tensor or it could be a new tensor.

Here’s few working examples :

In the above example, it can be noticed that we can specify the row x column shape of our want.

It should be noted that product of all the shapes needs to be the same as the total number of elements in the tensor.

In the second example, the rank of the tensor is increased to 4. There are 3 channels, where each row has 4 columns.

2. torch.unsqeeze(input, dim) -> tensor

unsqueeze() returns a tensor after adding a dimension of length one at a specified position. It shares the same data as the original tensor.

In the above example, we have added a new dimension at position zero.

In example 2, a new dimension is added at position 1.

3. torch.mm(matrix1, matrix2) -> tensor

The mm() performs matrix multiplication of two matrices. The matrix1 tensor is (n, m) shape, matrix2 is of (m, p) shape and output tensor will be (n, p) shape

In the first example, two tensors a, b with the shape (2, 4), (4, 3) are created respectively. mm() multiplies two matrices and returns a tensor with the shape of (2, 3).

The second example is similar to the first example, where the resulting matrix has a shape of (1, 2).

4. torch.mul(input, other) -> tensor

The function torch.mul() performs the element-wise multiplication of two tensors.

In the aforementioned example, we perform element wise matrix multiplication between tensors a , b.

As shown in the second example, element-wise multiplication is performed where matrix a is a FloatTensor, it could also be a DoubleTensor and the matrix b is an integer, it can also be any real number.

5 . torch.cross(matrix1, matrix2, dim = -1) -> tensor

cross() returns cross product of vectors in dimension of matrix1 and matrix2.

As shown above, cross() creates a tensor with the result of the cross product between two matrices a and b with the shapes of (2, 3)

Similar to example 1, a cross-product is performed on two matrices with random real numbers.

Conclusion

PyTorch offers many useful functions on tensors, I have linked some of the sources in the Resources section if you want to explore more operations on tensors.

This is my first post and I hope you liked it and learned something new after coming here. Please any kind of feedback is welcome, and don’t hesitate to share this with your friends! Thank You!!

Resources

Official Documentation

Fast.ai

What is PyTorch? by journaldev

Full code

--

--