Two-Dimensional Tensors in Pytorch

on

|

views

and

comments


Final Up to date 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 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 will be transformed to a two-dimensional tensor.

Within the earlier put up, we realized 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 be taught:

  • The best 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 various strategies to tensors reminiscent of, 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 components; they’re:

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

Sorts and Shapes of Two-Dimensional Tensors

Let’s first import a couple of crucial libraries we’ll use on this tutorial.

To verify the categories 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 reveal by changing a 2D checklist of integers to a 2D tensor object. For instance, we’ll create a 2D checklist and apply torch.tensor() for conversion.

As you possibly can see, the torch.tensor() technique 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 the way.

Changing Pandas Sequence to Two-Dimensional Tensors

Equally, we are able to 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, completely different parts in a tensor object will be accessed utilizing sq. brackets. You may merely put corresponding indices in sq. brackets to entry the specified parts in a tensor.

Within the under instance, we’ll create a tensor and entry sure parts utilizing two completely different strategies. Word that the index worth ought to at all times be one lower than the place the ingredient is positioned in a two-dimensional tensor.

What if we have to entry two or extra parts on the identical 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 a number 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 under 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 example, 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. Much 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).

Additional Studying

Developed similtaneously TensorFlow, PyTorch used to have an easier syntax till TensorFlow adopted Keras in its 2.x model. To be taught the fundamentals of PyTorch, you could need to learn the PyTorch tutorials:

Particularly 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 are appropriate for newcomers. A extra just lately revealed e-book needs to be advisable because the instruments and syntax are actively evolving. One instance is

Abstract

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

Particularly, you realized:

  • The best 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 various strategies to tensors reminiscent of, tensor addition, multiplication, and extra.
Share this
Tags

Must-read

Nvidia CEO reveals new ‘reasoning’ AI tech for self-driving vehicles | Nvidia

The billionaire boss of the chipmaker Nvidia, Jensen Huang, has unveiled new AI know-how that he says will assist self-driving vehicles assume like...

Tesla publishes analyst forecasts suggesting gross sales set to fall | Tesla

Tesla has taken the weird step of publishing gross sales forecasts that recommend 2025 deliveries might be decrease than anticipated and future years’...

5 tech tendencies we’ll be watching in 2026 | Expertise

Hi there, and welcome to TechScape. I’m your host, Blake Montgomery, wishing you a cheerful New Yr’s Eve full of cheer, champagne and...

Recent articles

More like this

LEAVE A REPLY

Please enter your comment!
Please enter your name here