One-Dimensional Tensors in Pytorch – MachineLearningMastery.com

on

|

views

and

comments


Final Up to date on November 15, 2022

PyTorch is an open-source deep studying framework primarily based on Python language. It means that you can construct, prepare, and deploy deep studying fashions, providing plenty of versatility and effectivity.

PyTorch is primarily targeted on tensor operations whereas a tensor could be a quantity, matrix, or a multi-dimensional array.

On this tutorial, we’ll carry out some fundamental operations on one-dimensional tensors as they’re advanced mathematical objects and a vital a part of the PyTorch library. Due to this fact, earlier than going into the element and extra superior ideas, one ought to know the fundamentals.

After going via this tutorial, you’ll:

  • Perceive the fundamentals of one-dimensional tensor operations in PyTorch.
  • Learn about tensor sorts and shapes and carry out tensor slicing and indexing operations.
  • Be capable to apply some strategies on tensor objects, resembling imply, commonplace deviation, addition, multiplication, and extra.

Let’s get began.

One-Dimensional Tensors in Pytorch
Image by Jo Szczepanska. Some rights reserved.

Varieties and Shapes of One-Dimensional Tensors

First off, let’s import a couple of libraries we’ll use on this tutorial.

When you have expertise in different programming languages, the best strategy to perceive a tensor is to think about it as a multidimensional array. Due to this fact, a one-dimensional tensor is just a one-dimensional array, or a vector. As a way to convert an inventory of integers to tensor, apply torch.tensor() constructor. As an example, we’ll take an inventory of integers and convert it to varied tensor objects.

Additionally, you’ll be able to apply the identical methodology torch.tensor() to transform a float listing to a float tensor.

Notice that parts of an inventory that must be transformed right into a tensor should have the identical sort. Furthermore, if you wish to convert an inventory to a sure tensor sort, torch additionally means that you can try this. The code strains beneath, for instance, will convert an inventory of integers to a float tensor.

Equally, dimension() and ndimension() strategies help you discover the scale and dimensions of a tensor object.

For reshaping a tensor object, view() methodology may be utilized. It takes rows and columns as arguments. For instance, let’s use this methodology to reshape int_list_to_float_tensor.

As you’ll be able to see, the view() methodology has modified the scale of the tensor to torch.Measurement([4, 1]), with 4 rows and 1 column.

Whereas the variety of parts in a tensor object ought to stay fixed after view() methodology is utilized, you need to use -1 (resembling reshaped_tensor.view(-1, 1)) to reshape a dynamic-sized tensor.

Changing Numpy Arrays to Tensors

Pytorch additionally means that you can convert NumPy arrays to tensors. You should use torch.from_numpy for this operation. Let’s take a NumPy array and apply the operation.

Equally, you’ll be able to convert the tensor object again to a NumPy array. Let’s use the earlier instance to indicate the way it’s accomplished.

Changing Pandas Sequence to Tensors

You can even convert a pandas collection to a tensor. For this, first you’ll have to retailer the pandas collection with values() perform utilizing a NumPy array.

Moreover, the Pytorch framework permits us to do so much with tensors resembling its merchandise() methodology returns a python quantity from a tensor and tolist() methodology returns an inventory.

Indexing and Slicing in One-Dimensional Tensors

Indexing and slicing operations are nearly the identical in Pytorch as python. Due to this fact, the primary index all the time begins at 0 and the final index is lower than the entire size of the tensor. Use sq. brackets to entry any quantity in a tensor.

Like an inventory in python, you can too carry out slicing operations on the values in a tensor. Furthermore, the Pytorch library means that you can change sure values in a tensor as effectively.

Let’s take an instance to verify how these operations may be utilized.

Now, let’s change the worth at index 3 of example_tensor:

Some Features to Apply on One-Dimensional Tensors

On this part, we’ll overview some statistical strategies that may be utilized on tensor objects.

Min and Max Features

These two helpful strategies are employed to seek out the minimal and most worth in a tensor. Right here is how they work.

We’ll use a sample_tensor for instance to use these strategies.

Imply and Normal Deviation

Imply and commonplace deviation are sometimes used whereas doing statistical operations on tensors. You may apply these two metrics utilizing .imply() and .std() capabilities in Pytorch.

Let’s use an instance to see how these two metrics are calculated.

Easy Addition and Multiplication Operations on One-Dimensional Tensors

Addition and Multiplication operations may be simply utilized on tensors in Pytorch. On this part, we’ll create two one-dimensional tensors to reveal how these operations can be utilized.

To your comfort, beneath is all of the examples above tying collectively so you’ll be able to strive them in a single shot:

Additional Studying

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

Particularly the fundamentals of PyTorch tensor may 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 guide needs to be really helpful because the instruments and syntax are actively evolving. One instance is

Abstract

On this tutorial, you’ve found how you can use one-dimensional tensors in Pytorch.

Particularly, you discovered:

  • The fundamentals of one-dimensional tensor operations in PyTorch
  • About tensor sorts and shapes and how you can carry out tensor slicing and indexing operations
  • How one can apply some strategies on tensor objects, resembling imply, commonplace deviation, addition, and multiplication
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