Last Updated on November 15, 2022

Two-dimensional tensors are analogous to two-dimensional metrics. Like a two-dimensional metric, a two-dimensional tensor also has $n$ number of rows and columns.

Let’s take a gray-scale image as an example, which is a two-dimensional matrix of numeric values, commonly known as pixels. Ranging from ‘0’ to ‘255’, each number represents a pixel intensity value. Here, the lowest intensity number (which is ‘0’) represents black regions in the image while the highest intensity number (which is ‘255’) represents white regions in the image. Using the PyTorch framework, this two-dimensional image or matrix can be converted to a two-dimensional tensor.

In the previous post, we learned about one-dimensional tensors in PyTorch and applied some useful tensor operations. In this tutorial, we’ll apply those operations to two-dimensional tensors using the PyTorch library. Specifically, we’ll learn:

  • How to create two-dimensional tensors in PyTorch and explore their types and shapes.
  • About slicing and indexing operations on two-dimensional tensors in detail.
  • To apply a number of methods to tensors such as, tensor addition, multiplication, and more.

Let’s get started.

Two-Dimensional Tensors in Pytorch
Picture by dylan dolte. Some rights reserved.

Tutorial Overview

This tutorial is divided into parts; they are:

  • Types and shapes of two-dimensional tensors
  • Converting two-dimensional tensors into NumPy arrays
  • Converting pandas series to two-dimensional tensors
  • Indexing and slicing operations on two-dimensional tensors
  • Operations on two-dimensional tensors

Types and Shapes of Two-Dimensional Tensors

Let’s first import a few necessary libraries we’ll use in this tutorial.

To check the types and shapes of the two-dimensional tensors, we’ll use the same methods from PyTorch, introduced previously for one-dimensional tensors. But, should it work the same way it did for the one-dimensional tensors?

Let’s demonstrate by converting a 2D list of integers to a 2D tensor object. As an example, we’ll create a 2D list and apply torch.tensor() for conversion.

As you can see, the torch.tensor() method also works well for the two-dimensional tensors. Now, let’s use shape(), size(), and ndimension() methods to return the shape, size, and dimensions of a tensor object.

Converting Two-Dimensional Tensors to NumPy Arrays

PyTorch allows us to convert a two-dimensional tensor to a NumPy array and then back to a tensor. Let’s find out how.

Converting Pandas Series to Two-Dimensional Tensors

Similarly, we can also convert a pandas DataFrame to a tensor. As with the one-dimensional tensors, we’ll use the same steps for the conversion. Using values attribute we’ll get the NumPy array and then use torch.from_numpy that allows you to convert a pandas DataFrame to a tensor.

Here is how we’ll do it.

Indexing and Slicing Operations on Two-Dimensional Tensors

For indexing operations, different elements in a tensor object can be accessed using square brackets. You can simply put corresponding indices in square brackets to access the desired elements in a tensor.

In the below example, we’ll create a tensor and access certain elements using two different methods. Note that the index value should always be one less than where the element is located in a two-dimensional tensor.

What if we need to access two or more elements at the same time? That’s where tensor slicing comes into play. Let’s use the previous example to access first two elements of the second row and first three elements of the third row.

Operations on Two-Dimensional Tensors

While there are a lot of operations you can apply on two-dimensional tensors using the PyTorch framework, here, we’ll introduce you to tensor addition, and scalar and matrix multiplication.

Adding Two-Dimensional Tensors

Adding two tensors is similar to matrix addition. It’s quite a straight forward process as you simply need an addition (+) operator to perform the operation. Let’s add two tensors in the below example.

Scalar and Matrix Multiplication of Two-Dimensional Tensors

Scalar multiplication in two-dimensional tensors is also identical to scalar multiplication in matrices. For instance, by multiplying a tensor with a scalar, say a scalar 4, you’ll be multiplying every element in a tensor by 4.

Coming to the multiplication of the two-dimensional tensors, in PyTorch makes things easier for us. Similar to the matrix multiplication in linear algebra, number of columns in tensor object A (i.e. 2×3) must be equal to the number of rows in tensor object B (i.e. 3×2).

Further Reading

Developed at the same time as TensorFlow, PyTorch used to have a simpler syntax until TensorFlow adopted Keras in its 2.x version. To learn the basics of PyTorch, you may want to read the PyTorch tutorials:

Especially the basics of PyTorch tensor can be found in the Tensor tutorial page:

There are also quite a few books on PyTorch that are suitable for beginners. A more recently published book should be recommended as the tools and syntax are actively evolving. One example is


In this tutorial, you learned about two-dimensional tensors in PyTorch.

Specifically, you learned:

  • How to create two-dimensional tensors in PyTorch and explore their types and shapes.
  • About slicing and indexing operations on two-dimensional tensors in detail.
  • To apply a number of methods to tensors such as, tensor addition, multiplication, and more.

Source link