PyTorch is an open-source deep learning framework based on Python language. It allows you to build, train, and deploy deep learning models, offering a lot of versatility and efficiency.

PyTorch is primarily focused on tensor operations while a tensor can be a number, matrix, or a multi-dimensional array.

In this tutorial, we will perform some basic operations on one-dimensional tensors as they are complex mathematical objects and an essential part of the PyTorch library. Therefore, before going into the detail and more advanced concepts, one should know the basics.

After going through this tutorial, you will:

  • Understand the basics of one-dimensional tensor operations in PyTorch.
  • Know about tensor types and shapes and perform tensor slicing and indexing operations.
  • Be able to apply some methods on tensor objects, such as mean, standard deviation, addition, multiplication, and more.

Let’s get started.

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 few libraries we’ll use in this tutorial.

If you have experience in other programming languages, the easiest way to understand a tensor is to consider it as a multidimensional array. Therefore, a one-dimensional tensor is simply a one-dimensional array, or a vector. In order to convert a list of integers to tensor, apply torch.tensor() constructor. For instance, we’ll take a list of integers and convert it to various tensor objects.

Also, you can apply the same method torch.tensor() to convert a float list to a float tensor.

Note that elements of a list that need to be converted into a tensor must have the same type. Moreover, if you want to convert a list to a certain tensor type, torch also allows you to do that. The code lines below, for example, will convert a list of integers to a float tensor.

Similarly, size() and ndimension() methods allow you to find the size and dimensions of a tensor object.

For reshaping a tensor object, view() method can be applied. It takes rows and columns as arguments. As an example, let’s use this method to reshape int_list_to_float_tensor.

As you can see, the view() method has changed the size of the tensor to torch.Size([4, 1]), with 4 rows and 1 column.

While the number of elements in a tensor object should remain constant after view() method is applied, you can use -1 (such as reshaped_tensor.view(-1, 1)) to reshape a dynamic-sized tensor.

Converting Numpy Arrays to Tensors

Pytorch also allows you to 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 can convert the tensor object back to a NumPy array. Let’s use the previous example to show how it’s done.

Converting Pandas Series to Tensors

You can also convert a pandas series to a tensor. For this, first you’ll need to store the pandas series with values() function using a NumPy array.

Furthermore, the Pytorch framework allows us to do a lot with tensors such as its item() method returns a python number from a tensor and tolist() method returns a list.

Indexing and Slicing in One-Dimensional Tensors

Indexing and slicing operations are almost the same in Pytorch as python. Therefore, the first index always starts at 0 and the last index is less than the total length of the tensor. Use square brackets to access any number in a tensor.

Like a list in python, you can also perform slicing operations on the values in a tensor. Moreover, the Pytorch library allows you to change certain values in a tensor as well.

Let’s take an example to check how these operations can be applied.

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

Some Functions to Apply on One-Dimensional Tensors

In this section, we’ll review some statistical methods that can be applied on tensor objects.

Min and Max Functions

These two useful methods are employed to find the minimum and maximum value in a tensor. Here is how they work.

We’ll use a sample_tensor as an example to apply these methods.

Mean and Standard Deviation

Mean and standard deviation are often used while doing statistical operations on tensors. You can apply these two metrics using .mean() and .std() functions in Pytorch.

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

Simple Addition and Multiplication Operations on One-Dimensional Tensors

Addition and Multiplication operations can be easily applied on tensors in Pytorch. In this section, we’ll create two one-dimensional tensors to demonstrate how these operations can be used.

For your convenience, below is all the examples above tying together so you can try them in one shot:

Further Reading

Developed at the same time as TensorFlow, PyTorch used to have a simpler syntax until TensorFlow adopted Keras in its 2.x version. To learn the basics of PyTorch, you may want to read the PyTorch tutorials:

Especially the basics of PyTorch tensor can be found in the Tensor tutorial page:

There are also quite a few books on PyTorch that are suitable for beginners. A more recently published book should be recommended as the tools and syntax are actively evolving. One example is

Summary

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

Specifically, you learned:

  • The basics of one-dimensional tensor operations in PyTorch
  • About tensor types and shapes and how to perform tensor slicing and indexing operations
  • How to apply some methods on tensor objects, such as mean, standard deviation, addition, and multiplication