HomeArtificial IntelligenceTwo-Dimensional Tensors in Pytorch

# Two-Dimensional Tensors in Pytorch

Final Up to date on January 14, 2022

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

Letâ€™s take a gray-scale picture for instance, which is a two-dimensional matrix of numeric values, generally often known as pixels. Starting from â€˜0â€™ to â€˜255â€™, every quantity represents a pixel depth worth. Right here, the bottom depth quantity (which is â€˜0â€™) represents black areas within the picture whereas the best depth quantity (which is â€˜255â€™) represents white areas within the picture. Utilizing the PyTorch framework, this two-dimensional picture or matrix might be transformed to a two-dimensional tensor.

Within the earlier put up, we discovered about one-dimensional tensors in PyTorch and utilized some helpful tensor operations. On this tutorial, weâ€™ll apply these operations to two-dimensional tensors utilizing the PyTorch library. Particularly, weâ€™ll study:

• The way to create two-dimensional tensors in PyTorch and discover their sorts and shapes.
• About slicing and indexing operations on two-dimensional tensors intimately.
• To use plenty of strategies to tensors comparable to, tensor addition, multiplication, and extra.

Letâ€™s get began.

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

## Tutorial Overview

This tutorial is split into elements; they’re:

• Varieties and shapes of two-dimensional tensors
• Changing two-dimensional tensors into NumPy arrays
• Changing pandas sequence to two-dimensional tensors
• Indexing and slicing operations on two-dimensional tensors
• Operations on two-dimensional tensors

## Varieties and Shapes of Two-Dimensional Tensors

Letâ€™s first import a number of essential libraries weâ€™ll use on this tutorial.

To verify the kinds and shapes of the two-dimensional tensors, weâ€™ll use the identical strategies from PyTorch, launched beforehand for one-dimensional tensors. However, ought to it work the identical approach it did for the one-dimensional tensors?

Letâ€™s display by changing a 2D listing of integers to a 2D tensor object. For example, weâ€™ll create a 2D listing and apply `torch.tensor()` for conversion.

As you possibly can see, the `torch.tensor()` methodology additionally works properly for the two-dimensional tensors. Now, letâ€™s use `form()`, `measurement()`, and `ndimension()` strategies to return the form, measurement, and dimensions of a tensor object.

## Changing Two-Dimensional Tensors to NumPy Arrays

PyTorch permits us to transform a two-dimensional tensor to a NumPy array after which again to a tensor. Letâ€™s learn how.

## Changing Pandas Collection to Two-Dimensional Tensors

Equally, we will additionally convert a pandas DataFrame to a tensor. As with the one-dimensional tensors, weâ€™ll use the identical steps for the conversion. Utilizing values attribute weâ€™ll get the NumPy array after which use `torch.from_numpy` that permits you to convert a pandas DataFrame to a tensor.

Right here is how weâ€™ll do it.

## Indexing and Slicing Operations on Two-Dimensional Tensors

For indexing operations, totally different parts in a tensor object might be accessed utilizing sq. brackets. You may merely put corresponding indices in sq. brackets to entry the specified parts in a tensor.

Within the beneath instance, weâ€™ll create a tensor and entry sure parts utilizing two totally different strategies. Observe that the index worth ought to all the time be one lower than the place the ingredient is situated in a two-dimensional tensor.

What if we have to entry two or extra parts on the similar time? Thatâ€™s the place tensor slicing comes into play. Letâ€™s use the earlier instance to entry first two parts of the second row and first three parts of the third row.

## Operations on Two-Dimensional Tensors

Whereas there are plenty of operations you possibly can apply on two-dimensional tensors utilizing the PyTorch framework, right here, weâ€™ll introduce you to tensor addition, and scalar and matrix multiplication.

### Including Two-Dimensional Tensors

Including two tensors is just like matrix addition. Itâ€™s fairly a straight ahead course of as you merely want an addition (+) operator to carry out the operation. Letâ€™s add two tensors within the beneath instance.

### Scalar and Matrix Multiplication of Two-Dimensional Tensors

Scalar multiplication in two-dimensional tensors can also be equivalent to scalar multiplication in matrices. As an illustration, by multiplying a tensor with a scalar, say a scalar 4, youâ€™ll be multiplying each ingredient in a tensor by 4.

Coming to the multiplication of the two-dimensional tensors, `torch.mm()` in PyTorch makes issues simpler for us. Just like the matrix multiplication in linear algebra, variety of columns in tensor object A (i.e. 2Ã—3) should be equal to the variety of rows in tensor object B (i.e. 3Ã—2).

Developed similtaneously TensorFlow, PyTorch used to have a less complicated syntax till TensorFlow adopted Keras in its 2.x model. To study the fundamentals of PyTorch, you might wish to learn the PyTorch tutorials:

Particularly the fundamentals of PyTorch tensor might be discovered within the Tensor tutorial web page:

There are additionally fairly a number of books on PyTorch which are appropriate for newbies. A extra not too long ago printed guide needs to be beneficial because the instruments and syntax are actively evolving. One instance is

## Abstract

On this tutorial, you discovered about two-dimensional tensors in PyTorch.

Particularly, you discovered:

• The way to create two-dimensional tensors in PyTorch and discover their sorts and shapes.
• About slicing and indexing operations on two-dimensional tensors intimately.
• To use plenty of strategies to tensors comparable to, tensor addition, multiplication, and extra.

RELATED ARTICLES