Thursday, April 30, 2026
HomeArtificial IntelligenceOne-Dimensional Tensors in Pytorch

One-Dimensional Tensors in Pytorch

[ad_1]

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

PyTorch is primarily centered on tensor operations whereas a tensor is usually 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 complicated mathematical objects and a necessary a part of the PyTorch library. Subsequently, 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.
  • Find out about tensor varieties and shapes and carry out tensor slicing and indexing operations.
  • Be capable of apply some strategies on tensor objects, similar to imply, customary deviation, addition, multiplication, and extra.

Let’s get began.

One-Dimensional Tensors in Pytorch

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.

When you have expertise in different programming languages, the best solution to perceive a tensor is to contemplate it as a multidimensional array. Subsequently, a one-dimensional tensor is just a one-dimensional array, or a vector. To be able to convert a listing of integers to tensor, apply torch.tensor() constructor. As an example, we’ll take a listing of integers and convert it to varied tensor objects.

Additionally, you possibly can apply the identical technique torch.tensor() to transform a float checklist to a float tensor.

Notice that components of a listing that have to be transformed right into a tensor will need to have the identical kind. Furthermore, if you wish to convert a listing to a sure tensor kind, torch additionally means that you can do this. The code strains under, for instance, will convert a listing of integers to a float tensor.

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

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

As you possibly can see, the view() technique has modified the scale 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() technique is utilized, you should use -1 (similar to 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 utilize torch.from_numpy for this operation. Let’s take a NumPy array and apply the operation.

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

Changing Pandas Collection to Tensors

It’s also possible to 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 quite a bit with tensors similar to its merchandise() technique returns a python quantity from a tensor and tolist() technique returns a listing.

Indexing and Slicing in One-Dimensional Tensors

Indexing and slicing operations are nearly the identical in Pytorch as python. Subsequently, the primary index at all times 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 a listing 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 examine how these operations could 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 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 customary deviation are sometimes used whereas doing statistical operations on tensors. You possibly can 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 could be simply utilized on tensors in Pytorch. On this part, we’ll create two one-dimensional tensors to show how these operations can be utilized.

On your comfort, under is all of the examples above tying collectively so you possibly can strive them in a single shot:

Additional Studying

Developed concurrently TensorFlow, PyTorch used to have a less complicated syntax till TensorFlow adopted Keras in its 2.x model. To be taught the fundamentals of PyTorch, chances are you’ll need to learn the PyTorch tutorials:

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

There are additionally fairly a couple of books on PyTorch which are appropriate for inexperienced persons. A extra not too long ago printed e-book ought to be beneficial because the instruments and syntax are actively evolving. One instance is

Abstract

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

Particularly, you realized:

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



[ad_2]

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments