Last Updated on April 8, 2023

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.

**Kick-start your project** with my book Deep Learning with PyTorch. It provides **self-study tutorials** with **working code**.

Let’s get started.

**Types and Shapes of One-Dimensional Tensors**

First off, letâ€™s import a few libraries weâ€™ll use in this tutorial.

1 2 3 |
import torch import numpy as np import pandas as pd |

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.

1 2 3 |
int_to_tensor = torch.tensor([10, 11, 12, 13]) print("Tensor object type after conversion: ", int_to_tensor.dtype) print("Tensor object type after conversion: ", int_to_tensor.type()) |

1 2 |
Tensor object type after conversion: torch.int64 Tensor object type after conversion: torch.LongTensor |

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

1 2 3 |
float_to_tensor = torch.tensor([10.0, 11.0, 12.0, 13.0]) print("Tensor object type after conversion: ", float_to_tensor.dtype) print("Tensor object type after conversion: ", float_to_tensor.type()) |

1 2 |
Tensor object type after conversion: torch.float32 Tensor object type after conversion: torch.FloatTensor |

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.

1 2 3 |
int_list_to_float_tensor = torch.FloatTensor([10, 11, 12, 13]) int_list_to_float_tensor.type() print("Tensor type after conversion: ", int_list_to_float_tensor.type()) |

1 |
Tensor type after conversion: torch.FloatTensor |

Similarly, `size()`

and `ndimension()`

methods allow you to find the size and dimensions of a tensor object.

1 2 |
print("Size of the int_list_to_float_tensor: ", int_list_to_float_tensor.size()) print("Dimensions of the int_list_to_float_tensor: ",int_list_to_float_tensor.ndimension()) |

1 2 |
Size of the int_list_to_float_tensor: torch.Size([4]) Dimensions of the int_list_to_float_tensor: 1 |

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`

.

1 2 3 |
reshaped_tensor = int_list_to_float_tensor.view(4, 1) print("Original Size of the tensor: ", reshaped_tensor) print("New size of the tensor: ", reshaped_tensor) |

1 2 3 4 5 6 7 8 |
Original Size of the tensor: tensor([[10.], [11.], [12.], [13.]]) New size of the tensor: tensor([[10.], [11.], [12.], [13.]]) |

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`

) to reshape a dynamic-sized tensor.**.**view(-1, 1)

**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.

1 2 3 4 5 |
numpy_arr = np.array([10.0, 11.0, 12.0, 13.0]) from_numpy_to_tensor = torch.from_numpy(numpy_arr) print("dtype of the tensor: ", from_numpy_to_tensor.dtype) print("type of the tensor: ", from_numpy_to_tensor.type()) |

1 2 |
dtype of the tensor: torch.float64 type of the tensor: torch.DoubleTensor |

Similarly, you can convert the tensor object back to a NumPy array. Letâ€™s use the previous example to show how itâ€™s done.

1 2 3 |
tensor_to_numpy = from_numpy_to_tensor.numpy() print("back to numpy from tensor: ", tensor_to_numpy) print("dtype of converted numpy array: ", tensor_to_numpy.dtype) |

1 2 |
back to numpy from tensor: [10. 11. 12. 13.] dtype of converted numpy array: float64 |

**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.

1 2 3 4 5 |
pandas_series=pd.Series([1, 0.2, 3, 13.1]) store_with_numpy=torch.from_numpy(pandas_series.values) print("Stored tensor in numpy array: ", store_with_numpy) print("dtype of stored tensor: ", store_with_numpy.dtype) print("type of stored tensor: ", store_with_numpy.type()) |

1 2 3 |
Stored tensor in numpy array: tensor([ 1.0000, 0.2000, 3.0000, 13.1000], dtype=torch.float64) dtype of stored tensor: torch.float64 type of stored tensor: torch.DoubleTensor |

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.

1 2 3 4 |
new_tensor=torch.tensor([10, 11, 12, 13]) print("the second item is",new_tensor[1].item()) tensor_to_list=new_tensor.tolist() print('tensor:', new_tensor,"\nlist:",tensor_to_list) |

1 2 3 |
the second item is 11 tensor: tensor([10, 11, 12, 13]) list: [10, 11, 12, 13] |

**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.

1 2 3 |
tensor_index = torch.tensor([0, 1, 2, 3]) print("Check value at index 0:",tensor_index[0]) print("Check value at index 3:",tensor_index[3]) |

1 2 |
Check value at index 0: tensor(0) Check value at index 3: tensor(3) |

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.

1 2 3 4 |
example_tensor = torch.tensor([50, 11, 22, 33, 44]) sclicing_tensor = example_tensor[1:4] print("example tensor : ", example_tensor) print("subset of example tensor:", sclicing_tensor) |

1 2 |
example tensor : tensor([50, 11, 22, 33, 44]) subset of example tensor: tensor([11, 22, 33]) |

Now, letâ€™s change the value at index 3 of `example_tensor`

:

1 2 3 |
print("value at index 3 of example tensor:", example_tensor[3]) example_tensor[3] = 0 print("new tensor:", example_tensor) |

1 2 |
value at index 3 of example tensor: tensor(0) new tensor: tensor([50, 11, 22, 0, 44]) |

### Want to Get Started With Deep Learning with PyTorch?

Take my free email crash course now (with sample code).

Click to sign-up and also get a free PDF Ebook version of the course.

**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.

1 2 3 4 5 |
sample_tensor = torch.tensor([5, 4, 3, 2, 1]) min_value = sample_tensor.min() max_value = sample_tensor.max() print("check minimum value in the tensor: ", min_value) print("check maximum value in the tensor: ", max_value) |

1 2 |
check minimum value in the tensor: tensor(1) check maximum value in the tensor: tensor(5) |

**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.

1 2 3 4 5 |
mean_std_tensor = torch.tensor([-1.0, 2.0, 1, -2]) Mean = mean_std_tensor.mean() print("mean of mean_std_tensor: ", Mean) std_dev = mean_std_tensor.std() print("standard deviation of mean_std_tensor: ", std_dev) |

1 2 |
mean of mean_std_tensor: tensor(0.) standard deviation of mean_std_tensor: tensor(1.8257) |

**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.

1 2 3 4 5 6 7 8 |
a = torch.tensor([1, 1]) b = torch.tensor([2, 2]) add = a + b multiply = a * b print("addition of two tensors: ", add) print("multiplication of two tensors: ", multiply) |

1 2 |
addition of two tensors: tensor([3, 3]) multiplication of two tensors: tensor([2, 2]) |

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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
import torch import numpy as np import pandas as pd int_to_tensor = torch.tensor([10, 11, 12, 13]) print("Tensor object type after conversion: ", int_to_tensor.dtype) print("Tensor object type after conversion: ", int_to_tensor.type()) float_to_tensor = torch.tensor([10.0, 11.0, 12.0, 13.0]) print("Tensor object type after conversion: ", float_to_tensor.dtype) print("Tensor object type after conversion: ", float_to_tensor.type()) int_list_to_float_tensor = torch.FloatTensor([10, 11, 12, 13]) int_list_to_float_tensor.type() print("Tensor type after conversion: ", int_list_to_float_tensor.type()) print("Size of the int_list_to_float_tensor: ", int_list_to_float_tensor.size()) print("Dimensions of the int_list_to_float_tensor: ",int_list_to_float_tensor.ndimension()) reshaped_tensor = int_list_to_float_tensor.view(4, 1) print("Original Size of the tensor: ", reshaped_tensor) print("New size of the tensor: ", reshaped_tensor) numpy_arr = np.array([10.0, 11.0, 12.0, 13.0]) from_numpy_to_tensor = torch.from_numpy(numpy_arr) print("dtype of the tensor: ", from_numpy_to_tensor.dtype) print("type of the tensor: ", from_numpy_to_tensor.type()) tensor_to_numpy = from_numpy_to_tensor.numpy() print("back to numpy from tensor: ", tensor_to_numpy) print("dtype of converted numpy array: ", tensor_to_numpy.dtype) pandas_series=pd.Series([1, 0.2, 3, 13.1]) store_with_numpy=torch.from_numpy(pandas_series.values) print("Stored tensor in numpy array: ", store_with_numpy) print("dtype of stored tensor: ", store_with_numpy.dtype) print("type of stored tensor: ", store_with_numpy.type()) new_tensor=torch.tensor([10, 11, 12, 13]) print("the second item is",new_tensor[1].item()) tensor_to_list=new_tensor.tolist() print('tensor:', new_tensor,"\nlist:",tensor_to_list) tensor_index = torch.tensor([0, 1, 2, 3]) print("Check value at index 0:",tensor_index[0]) print("Check value at index 3:",tensor_index[3]) example_tensor = torch.tensor([50, 11, 22, 33, 44]) sclicing_tensor = example_tensor[1:4] print("example tensor : ", example_tensor) print("subset of example tensor:", sclicing_tensor) print("value at index 3 of example tensor:", example_tensor[3]) example_tensor[3] = 0 print("new tensor:", example_tensor) sample_tensor = torch.tensor([5, 4, 3, 2, 1]) min_value = sample_tensor.min() max_value = sample_tensor.max() print("check minimum value in the tensor: ", min_value) print("check maximum value in the tensor: ", max_value) mean_std_tensor = torch.tensor([-1.0, 2.0, 1, -2]) Mean = mean_std_tensor.mean() print("mean of mean_std_tensor: ", Mean) std_dev = mean_std_tensor.std() print("standard deviation of mean_std_tensor: ", std_dev) a = torch.tensor([1, 1]) b = torch.tensor([2, 2]) add = a + b multiply = a * b print("addition of two tensors: ", add) print("multiplication of two tensors: ", multiply) |

## 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

- Deep Learning with PyTorch by Eli Stevens, Luca Antiga, and Thomas Viehmann, 2020.

https://www.manning.com/books/deep-learning-with-pytorch

**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

Great Article! Thanks for writing on this topic.

You are very welcome Kevin!

Thank you!