# Two-Dimensional Tensors in Pytorch

Last Updated on April 8, 2023

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.

Kick-start your project with my book Deep Learning with PyTorch. It provides self-study tutorials with working code.

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.

### Want to Get Started With Deep Learning with PyTorch?

Take my free email crash course now (with sample code).

Click to sign-up and also get a free PDF Ebook version of the course.

## 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 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, torch.mm() 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).

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

## Summary

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.

## Get Started on Deep Learning with PyTorch! #### Learn how to build deep learning models

...using the newly released PyTorch 2.0 library

Discover how in my new Ebook:
Deep Learning with PyTorch

It provides self-study tutorials with hundreds of working code to turn you from a novice to expert. It equips you with
tensor operation, training, evaluation, hyperparameter optimization, and much more...

### 8 Responses to Two-Dimensional Tensors in Pytorch

1. Will Li April 12, 2022 at 11:30 am #

Thank you for the article. However, I would suggest you double check the display of the outputs in this article as there are at least two places showing inputs where it should show outputs.

• James Carmichael April 14, 2022 at 2:47 am #

Thank you for the feedback Will!

2. Prakriti July 12, 2022 at 1:07 am #

Thank you so much. I’m a beginner and this article helped a lot.

• James Carmichael July 12, 2022 at 9:05 am #

You are very welcome Prakriti! We greatly appreciate your feedback and support!

• Alger November 21, 2022 at 1:23 pm #

I have a question I want to ask, I use 1000 matrices of 64×64 as input, and the output is also 64×64 matrices, can it be achieved?

3. Alger November 21, 2022 at 12:59 pm #

I have a question I want to ask, I use 1000 matrices of 64×64 as input, and the output is also 64×64 matrices, can it be achieved? Thank you.

4. pan xinghai November 21, 2022 at 1:36 pm #

I have a question I want to ask, I use 1000 matrices of 64×64 as input, and the output is also 64×64 matrices, can it be achieved?