One-Dimensional Tensors in Pytorch

0
109
One-Dimensional Tensors in Pytorch


Last Updated 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 quite a lot of versatility and effectivity.

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

In this tutorial, we are going to carry out some primary operations on one-dimensional tensors as they’re advanced mathematical objects and a necessary a part of the PyTorch library. Therefore, earlier than going into the element and extra superior ideas, one ought to know the fundamentals.

After going by means of this tutorial, you’ll:

  • Understand the fundamentals of one-dimensional tensor operations in PyTorch.
  • Know about tensor sorts and shapes and carry out tensor slicing and indexing operations.
  • Be in a position to apply some strategies on tensor objects, reminiscent of imply, commonplace deviation, addition, multiplication, and extra.

Let’s get began.

One-Dimensional Tensors in Pytorch

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

Types and Shapes of One-Dimensional Tensors

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

If you may have expertise in different programming languages, the simplest method to perceive a tensor is to contemplate it as a multidimensional array. Therefore, a one-dimensional tensor is solely a one-dimensional array, or a vector. In order to transform an inventory of integers to tensor, apply torch.tensor() constructor. For occasion, we’ll take an inventory of integers and convert it to numerous tensor objects.

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

Note that parts of an inventory that should be transformed right into a tensor will need to have the identical kind. Moreover, if you wish to convert an inventory to a sure tensor kind, torch additionally means that you can try this. The code traces under, for instance, will convert an inventory of integers to a float tensor.

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

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

As you possibly can see, the view() technique has modified the dimensions of the tensor to torch.Size([4, 1]), with 4 rows and 1 column.

While the variety of parts in a tensor object ought to stay fixed after view() technique is utilized, you should use -1 (reminiscent of reshaped_tensor.view(-1, 1)) to reshape a dynamic-sized tensor.

Converting Numpy Arrays to Tensors

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

Similarly, you possibly can convert the tensor object again to a NumPy array. Let’s use the earlier instance to point out the way it’s completed.

Converting Pandas Series to Tensors

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

Furthermore, the Pytorch framework permits us to do loads with tensors reminiscent of its merchandise() technique returns a python quantity from a tensor and tolist() technique returns an inventory.

Indexing and Slicing in One-Dimensional Tensors

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

Like an inventory in python, you may as well carry out slicing operations on the values in a tensor. Moreover, the Pytorch library means that you can change sure values in a tensor as nicely.

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

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

Some Functions to Apply on One-Dimensional Tensors

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

Min and Max Functions

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

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

Mean and Standard Deviation

Mean and commonplace deviation are sometimes used whereas doing statistical operations on tensors. You 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.

Simple Addition and Multiplication Operations on One-Dimensional Tensors

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

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

Further Reading

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

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

There are additionally fairly a number of books on PyTorch which might be appropriate for novices. A extra just lately revealed ebook must be really useful because the instruments and syntax are actively evolving. One instance is

Summary

In this tutorial, you’ve found learn how to use one-dimensional tensors in Pytorch.

Specifically, you discovered:

  • The fundamentals of one-dimensional tensor operations in PyTorch
  • About tensor sorts and shapes and learn how to carry out tensor slicing and indexing operations
  • How to use some strategies on tensor objects, reminiscent of imply, commonplace deviation, addition, and multiplication

LEAVE A REPLY

Please enter your comment!
Please enter your name here