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.

## 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.

import torch

import numpy as np

import pandas as pd

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.

example_2D_list = [[5, 10, 15, 20],

[25, 30, 35, 40],

[45, 50, 55, 60]]

list_to_tensor = torch.tensor(example_2D_list)

print(“Our New 2D Tensor from 2D List is: “, list_to_tensor)

Our New 2D Tensor from 2D List is: tensor([[ 5, 10, 15, 20],

[25, 30, 35, 40],

[45, 50, 55, 60]])

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.

print(“Getting the shape of tensor object: “, list_to_tensor.shape)

print(“Getting the size of tensor object: “, list_to_tensor.size())

print(“Getting the dimensions of tensor object: “, list_to_tensor.ndimension())

print(“Getting the shape of tensor object: “, list_to_tensor.shape)

print(“Getting the size of tensor object: “, list_to_tensor.size())

print(“Getting the dimensions of tensor object: “, list_to_tensor.ndimension())

**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 two_D tensor to numpy array

twoD_tensor_to_numpy = list_to_tensor.numpy()

print(“Converting two_Dimensional tensor to numpy array:”)

print(“Numpy array after conversion: “, twoD_tensor_to_numpy)

print(“Data type after conversion: “, twoD_tensor_to_numpy.dtype)

print(“***************************************************************”)

# Converting numpy array back to a tensor

back_to_tensor = torch.from_numpy(twoD_tensor_to_numpy)

print(“Converting numpy array back to two_Dimensional tensor:”)

print(“Tensor after conversion:”, back_to_tensor)

print(“Data type after conversion: “, back_to_tensor.dtype)

Converting two_Dimensional tensor to numpy array:

Numpy array after conversion: [[ 5 10 15 20]

[25 30 35 40]

[45 50 55 60]]

Data type after conversion: int64

***************************************************************

Converting numpy array back to two_Dimensional tensor:

Tensor after conversion: tensor([[ 5, 10, 15, 20],

[25, 30, 35, 40],

[45, 50, 55, 60]])

Data type after conversion: torch.int64

**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.

# Converting Pandas Dataframe to a Tensor

dataframe = pd.DataFrame({‘x’:[22,24,26],’y’:[42,52,62]})

print(“Pandas to numpy conversion: “, dataframe.values)

print(“Data type before tensor conversion: “, dataframe.values.dtype)

print(“***********************************************”)

pandas_to_tensor = torch.from_numpy(dataframe.values)

print(“Getting new tensor: “, pandas_to_tensor)

print(“Data type after conversion to tensor: “, pandas_to_tensor.dtype)

Pandas to numpy conversion: [[22 42]

[24 52]

[26 62]]

Data type before tensor conversion: int64

***********************************************

Getting new tensor: tensor([[22, 42],

[24, 52],

[26, 62]])

Data type after conversion to tensor: torch.int64

**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.

example_tensor = torch.tensor([[10, 20, 30, 40],

[50, 60, 70, 80],

[90, 100, 110, 120]])

print(“Accessing element in 2nd row and 2nd column: “, example_tensor[1, 1])

print(“Accessing element in 2nd row and 2nd column: “, example_tensor[1][1])

print(“********************************************************”)

print(“Accessing element in 3rd row and 4th column: “, example_tensor[2, 3])

print(“Accessing element in 3rd row and 4th column: “, example_tensor[2][3])

Accessing element in 2nd row and 2nd column: tensor(60)

Accessing element in 2nd row and 2nd column: tensor(60)

********************************************************

Accessing element in 3rd row and 4th column: tensor(120)

Accessing element in 3rd row and 4th column: tensor(120)

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.

example_tensor = torch.tensor([[10, 20, 30, 40],

[50, 60, 70, 80],

[90, 100, 110, 120]])

print(“Accessing first two elements of the second row: “, example_tensor[1, 0:2])

print(“Accessing first two elements of the second row: “, example_tensor[1][0:2])

print(“********************************************************”)

print(“Accessing first three elements of the third row: “, example_tensor[2, 0:3])

print(“Accessing first three elements of the third row: “, example_tensor[2][0:3])

example_tensor = torch.tensor([[10, 20, 30, 40],

[50, 60, 70, 80],

[90, 100, 110, 120]])

print(“Accessing first two elements of the second row: “, example_tensor[1, 0:2])

print(“Accessing first two elements of the second row: “, example_tensor[1][0:2])

print(“********************************************************”)

print(“Accessing first three elements of the third row: “, example_tensor[2, 0:3])

print(“Accessing first three elements of the third row: “, example_tensor[2][0:3])

**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.

A = torch.tensor([[5, 10],

[50, 60],

[100, 200]])

B = torch.tensor([[10, 20],

[60, 70],

[200, 300]])

add = A + B

print(“Adding A and B to get: “, add)

Adding A and B to get: tensor([[ 15, 30],

[110, 130],

[300, 500]])

**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.

new_tensor = torch.tensor([[1, 2, 3],

[4, 5, 6]])

mul_scalar = 4 * new_tensor

print(“result of scalar multiplication: “, mul_scalar)

result of scalar multiplication: tensor([[ 4, 8, 12],

[16, 20, 24]])

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).

A = torch.tensor([[3, 2, 1],

[1, 2, 1]])

B = torch.tensor([[3, 2],

[1, 1],

[2, 1]])

A_mult_B = torch.mm(A, B)

print(“multiplying A with B: “, A_mult_B)

multiplying A with B: tensor([[13, 9],

[ 7, 5]])

## 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:

https://pytorch.org/tutorials/

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

https://pytorch.org/tutorials/beginner/basics/tensorqs_tutorial.html

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

Deep Learning with PyTorch by Eli Stevens, Luca Antiga, and Thomas Viehmann, 2020.

https://www.manning.com/books/deep-learning-with-pytorch

**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.

The post Two-Dimensional Tensors in Pytorch appeared first on Machine Learning Mastery.

Read MoreMachine Learning Mastery