Two-Dimensional Tensors in Pytorch

0
120
Two-Dimensional Tensors in Pytorch


Last Updated on November 15, 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 generally known as pixels. Ranging from ‘0’ to ‘255’, every quantity represents a pixel depth worth. 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. Using the PyTorch framework, this two-dimensional picture or matrix will be transformed to a two-dimensional tensor.

In the earlier submit, we discovered about one-dimensional tensors in PyTorch and utilized some helpful tensor operations. In this tutorial, we’ll apply these operations to two-dimensional tensors utilizing the PyTorch library. Specifically, we’ll study:

  • How to create two-dimensional tensors in PyTorch and discover their sorts and shapes.
  • About slicing and indexing operations on two-dimensional tensors intimately.
  • To apply various strategies to tensors akin to, tensor addition, multiplication, and extra.

Let’s get began.

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

Tutorial Overview

This tutorial is split into components; they’re:

  • Types and shapes of two-dimensional tensors
  • Converting two-dimensional tensors into NumPy arrays
  • Converting pandas sequence 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 couple of vital 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, introduced beforehand for one-dimensional tensors. But, ought to it work the identical means it did for the one-dimensional tensors?

Let’s exhibit by changing a 2D listing of integers to a 2D tensor object. As an instance, we’ll create a 2D listing and apply torch.tensor() for conversion.

As you may see, the torch.tensor() technique additionally works nicely for the two-dimensional tensors. Now, let’s use form(), dimension(), and ndimension() strategies to return the form, dimension, and dimensions of a tensor object.

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

Converting Pandas Series to Two-Dimensional Tensors

Similarly, we are able to additionally convert a pandas DataBody to a tensor. As with the one-dimensional tensors, we’ll use the identical steps for the conversion. Using values attribute we’ll get the NumPy array after which use torch.from_numpy that lets you convert a pandas DataBody to a tensor.

Here is how we’ll do it.

Indexing and Slicing Operations on Two-Dimensional Tensors

For indexing operations, completely different components in a tensor object will be accessed utilizing sq. brackets. You can merely put corresponding indices in sq. brackets to entry the specified components in a tensor.

In the beneath instance, we’ll create a tensor and entry sure components utilizing two completely different strategies. Note that the index worth ought to at all times be one lower than the place the ingredient is situated in a two-dimensional tensor.

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

Operations on Two-Dimensional Tensors

While there are loads of operations you may apply on two-dimensional tensors utilizing the PyTorch framework, right here, we’ll introduce you to tensor addition, and scalar and matrix multiplication.

Adding Two-Dimensional Tensors

Adding 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 similar to scalar multiplication in matrices. For occasion, 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. Similar to 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).

Further Reading

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

Especially the fundamentals of PyTorch tensor will be discovered within the Tensor tutorial web page:

There are additionally fairly a couple of books on PyTorch which can be appropriate for freshmen. A extra not too long ago revealed ebook ought to be beneficial because the instruments and syntax are actively evolving. One instance is

Summary

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

Specifically, you discovered:

  • How to create two-dimensional tensors in PyTorch and discover their sorts and shapes.
  • About slicing and indexing operations on two-dimensional tensors intimately.
  • To apply various strategies to tensors akin to, tensor addition, multiplication, and extra.

LEAVE A REPLY

Please enter your comment!
Please enter your name here