One-Dimensional Tensor in Pytorch
Last Updated :
29 Jun, 2021
In this article, we are going to discuss a one-dimensional tensor in Python. We will look into the following concepts:
- Creation of One-Dimensional Tensors
- Accessing Elements of Tensor
- Size of Tensor
- Data Types of Elements of Tensors
- View of Tensor
- Floating Point Tensor
Introduction
The Pytorch is used to process the tensors. Tensors are multidimensional arrays. PyTorch accelerates the scientific computation of tensors as it has various inbuilt functions.
Vector:
A vector is a one-dimensional tensor that holds elements of multiple data types. We can create vectors using PyTorch. Pytorch is available in the Python torch module. So we need to import it.
Syntax:
import pytorch
Creation of One-Dimensional Tensors:
One dimensional vector is created using the torch.tensor() method.
Syntax:
torch.tensor([element1,element2,.,element n])
Where elements are input elements to a tensor
Example: Python program to create tensor elements
Python3
# importing torch module
import torch
# create one dimensional tensor with integer type elements
a = torch.tensor([10, 20, 30, 40, 50])
print(a)
# create one dimensional tensor with float type elements
b = torch.tensor([10.12, 20.56, 30.00, 40.3, 50.4])
print(b)
Output:
tensor([10, 20, 30, 40, 50])
tensor([10.1200, 20.5600, 30.0000, 40.3000, 50.4000])
Accessing Elements of Tensor:
We can access the elements in the tensor vector using the index of elements.
Syntax:
tensor_name([index])
Where the index is the position of the element in the tensor:
- Indexing starts from 0 from first
- Indexing starts from -1 from last
Example: Python program to access elements using the index.
Python3
# importing torch module
import torch
# create one dimensional tensor with integer type elements
a = torch.tensor([10, 20, 30, 40, 50])
# get 0 and 1 index elements
print(a[0], a[1])
# get 4 th index element
print(a[4])
# get 4 index element from last
print(a[-4])
# get 2 index element from last
print(a[-2])
Output:
tensor(10) tensor(20)
tensor(50)
tensor(20)
tensor(40)
We can access n elements at a time using the”:" operator, This is known as slicing.
Syntax:
tensor([start_index:end_index])
Where start_index is the starting index and end_index is the ending index.
Example: Python program to access multiple elements.
Python3
# importing torch module
import torch
# create one dimensional tensor with integer type elements
a = torch.tensor([10, 20, 30, 40, 50])
# access elements from 1 to 4
print(a[1:4])
# access from 4
print(a[4:])
# access from last
print(a[-1:])
Output:
tensor([20, 30, 40])
tensor([50])
tensor([50])
Tensor size:
This is used to get the length(number of elements) in a tensor using the size() method.
Syntax:
tensor.size()
Example: Python program to get the tensor size.
Python3
# importing torch module
import torch
# create one dimensional tensor integer type elements
a = torch.FloatTensor([10, 20, 30, 40, 50])
# size of tensor
print(a.size())
# create one dimensional tensor integer type elements
b = torch.FloatTensor([10, 20, 30, 40, 50, 45, 67, 43])
# size of tensor
print(b.size())
Output:
torch.Size([5])
torch.Size([8])
Data Types of Elements of Tensors:
We can get the data type of the tensor data elements. Then dtype() is used to get the data type of the tensor
Syntax:
tensor_vector.dtype
Where tensor_vector is the one-dimensional tensor vector.
Example:
Python3
# importing torch module
import torch
# create one dimensional tensor with integer type elements
a = torch.tensor([10, 20, 30, 40, 50])
# get data type of vector a
print(a.dtype)
# create one dimensional tensor with float type elements
b = torch.tensor([10.12, 20.56, 30.00, 40.3, 50.4])
# get data type of vector b
print(b.dtype)
Output:
torch.int64
torch.float32
View of Tensor:
The view() is used to view the tensor in two-dimensional format ie, rows and columns. We have to specify the number of rows and the number of columns to be viewed.
Syntax:
tensor.view(no_of_rows,no_of_columns)
Where,
- tensor is an input one-dimensional tensor
- no_of_rows is the total number of the rows that the tensor is viewed
- no_of_columns is the total number of the columns that the tensor is viewed
Example: Python program to create a tensor with 10 elements and view with 5 rows and 2 columns and vice versa.
Python3
# importing torch module
import torch
# create one dimensional tensor 10 elements
a = torch.FloatTensor([10, 20, 30, 40, 50, 1, 2, 3, 4, 5])
# view tensor in 5 rows and 2 columns
print(a.view(5, 2))
# view tensor in 2 rows and 5 columns
print(a.view(2, 5))
Output:
tensor([[10., 20.],
[30., 40.],
[50., 1.],
[ 2., 3.],
[ 4., 5.]])
tensor([[10., 20., 30., 40., 50.],
[ 1., 2., 3., 4., 5.]])
Floating-point tensor:
This tensor is used to define the elements with float type. We can create a floating-point Tensor using an integer element by using the FloatTensor property.
Syntax:
torch.FloatTensor([element1,element 2,.,element n])
Example: Python program to create float tensor and get elements.
Python3
# importing torch module
import torch
# create one dimensional Float Tensor with
# integer type elements
a = torch.FloatTensor([10, 20, 30, 40, 50])
# display data type
print(a.dtype)
# access elements from 0 to 3
print(a[0:3])
# access from 4
print(a[4:])
Output:
torch.float32
tensor([10., 20., 30.])
tensor([50.])
Similar Reads
Two-Dimensional Tensors in Pytorch
PyTorch is a python library developed by Facebook to run and train machine learning and deep learning models. In PyTorch everything is based on tensor operations. Two-dimensional tensors are nothing but matrices or vectors of two-dimension with specific datatype, of n rows and n columns. Representat
3 min read
Tensor Operations in PyTorch
In this article, we will discuss tensor operations in PyTorch. PyTorch is a scientific package used to perform operations on the given data like tensor in python. A Tensor is a collection of data like a numpy array. We can create a tensor using the tensor function: Syntax: torch.tensor([[[element1,e
5 min read
Creating a Tensor in Pytorch
All the deep learning is computations on tensors, which are generalizations of a matrix that can be indexed in more than 2 dimensions. Tensors can be created from Python lists with the torch.tensor() function. The tensor() Method: To create tensors with Pytorch we can simply use the tensor() method:
6 min read
Python - PyTorch is_tensor() method
PyTorch torch.is_tensor() method returns True if the passed object is a PyTorch tensor. Syntax: torch.is_tensor(object) Arguments object: This is input tensor to be tested. Return: It returns either True or False. Let's see this concept with the help of few examples: Example 1: Python3 # Importing t
1 min read
Tensor.detach() Method in Python PyTorch
In this article, we will see Tensor.detach() method in PyTorch using Python. Pytorch is a Python and C++ interface for an open-source deep learning platform. It is found within the torch module. In PyTorch, the input data has to be processed in the form of a tensor. It also includes a module that ca
2 min read
Tensors in Pytorch
A Pytorch Tensor is basically the same as a NumPy array. This means it does not know anything about deep learning or computational graphs or gradients and is just a generic n-dimensional array to be used for arbitrary numeric computation. However, the biggest difference between a NumPy array and a P
6 min read
Change view of Tensor in PyTorch
In this article, we will learn how to change the shape of tensors using the PyTorch view function. We will also look at the multiple ways in which we can change the shape of the tensors. Also, we can use the view function to convert lower-dimensional matrices to higher dimensions. What is the necess
3 min read
How to perform element-wise division on tensors in PyTorch?
In this article, we will understand how to perform element-wise division of two tensors in PyTorch. To perform the element-wise division of tensors, we can apply the torch.div() method. It takes two tensors (dividend and divisor) as the inputs and returns a new tensor with the element-wise division
3 min read
Convert PyTorch Tensor to Python List
PyTorch, a widely-used open-source machine learning library, is known for its flexibility and ease of use in building deep learning models. A fundamental component of PyTorch is the tensor, a multi-dimensional array that serves as the primary data structure for model training and inference. However,
3 min read
How to perform element-wise addition on tensors in PyTorch?
In this article, we are going to see how to perform element-wise addition on tensors in PyTorch in Python. We can perform element-wise addition using torch.add() function. This function also allows us to perform addition on the same or different dimensions of tensors. If tensors are different in di
3 min read