One-Dimensional Tensors in Pytorch

Final Up to date on November 15, 2022

PyTorch is an open-source deep studying framework primarily based on Python language. It permits you to construct, prepare, and deploy deep studying fashions, providing loads of versatility and effectivity.

PyTorch is primarily centered on tensor operations whereas a tensor generally is a quantity, matrix, or a multi-dimensional array.

On this tutorial, we are going to carry out some fundamental operations on one-dimensional tensors as they’re advanced mathematical objects and an important 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 by way of this tutorial, you’ll:

  • Perceive the fundamentals of one-dimensional tensor operations in PyTorch.
  • Find out about tensor varieties and shapes and carry out tensor slicing and indexing operations.
  • Have the ability to apply some strategies on tensor objects, corresponding to imply, customary deviation, addition, multiplication, and extra.

Let’s get began.

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

Sorts and Shapes of One-Dimensional Tensors

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

You probably have expertise in different programming languages, the best technique to perceive a tensor is to contemplate 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. For example, we’ll take an inventory of integers and convert it to varied tensor objects.

Additionally, you may apply the identical methodology torch.tensor() to transform a float checklist to a float tensor.

Be aware that components of an inventory that should be transformed right into a tensor should have the identical kind. Furthermore, if you wish to convert an inventory to a sure tensor kind, torch additionally permits you to do this. The code strains under, for instance, will convert an inventory of integers to a float tensor.

Equally, measurement() and ndimension() strategies let you discover the dimensions and dimensions of a tensor object.

For reshaping a tensor object, view() methodology might 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 may see, the view() methodology has modified the dimensions of the tensor to torch.Measurement([4, 1]), with 4 rows and 1 column.

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

Changing Numpy Arrays to Tensors

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

Equally, you may convert the tensor object again to a NumPy array. Let’s use the earlier instance to indicate the way it’s achieved.

Changing Pandas Sequence to Tensors

You may as well convert a pandas collection to a tensor. For this, first you’ll must retailer the pandas collection with values() perform utilizing a NumPy array.

Moreover, the Pytorch framework permits us to do so much with tensors corresponding to 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 virtually the identical in Pytorch as python. Due to this fact, the primary index at all times begins at 0 and the final index is lower than the overall size of the tensor. Use sq. brackets to entry any quantity in a tensor.

Like an inventory in python, you can even carry out slicing operations on the values in a tensor. Furthermore, the Pytorch library permits you to change sure values in a tensor as properly.

Let’s take an instance to examine how these operations might be utilized.

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

Some Capabilities to Apply on One-Dimensional Tensors

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

Min and Max Capabilities

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

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

Imply and Commonplace Deviation

Imply and customary deviation are sometimes used whereas doing statistical operations on tensors. You’ll be able to 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 might be simply utilized on tensors in Pytorch. On this part, we’ll create two one-dimensional tensors to display how these operations can be utilized.

To your comfort, under is all of the examples above tying collectively so you may attempt them in a single shot:

Additional Studying

Developed similtaneously TensorFlow, PyTorch used to have an easier syntax till TensorFlow adopted Keras in its 2.x model. To study the fundamentals of PyTorch, chances are you’ll need 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 couple of books on PyTorch which might be appropriate for newbies. A extra not too long ago printed ebook must be advisable because the instruments and syntax are actively evolving. One instance is


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

Particularly, you realized:

  • The fundamentals of one-dimensional tensor operations in PyTorch
  • About tensor varieties and shapes and carry out tensor slicing and indexing operations
  • The right way to apply some strategies on tensor objects, corresponding to imply, customary deviation, addition, and multiplication

Newsletter Updates

Enter your email address below to subscribe to our newsletter

Leave a Reply