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
import torch
a = torch.tensor([ 10 , 20 , 30 , 40 , 50 ])
print (a)
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
import torch
a = torch.tensor([ 10 , 20 , 30 , 40 , 50 ])
print (a[ 0 ], a[ 1 ])
print (a[ 4 ])
print (a[ - 4 ])
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
import torch
a = torch.tensor([ 10 , 20 , 30 , 40 , 50 ])
print (a[ 1 : 4 ])
print (a[ 4 :])
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
import torch
a = torch.FloatTensor([ 10 , 20 , 30 , 40 , 50 ])
print (a.size())
b = torch.FloatTensor([ 10 , 20 , 30 , 40 , 50 , 45 , 67 , 43 ])
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
import torch
a = torch.tensor([ 10 , 20 , 30 , 40 , 50 ])
print (a.dtype)
b = torch.tensor([ 10.12 , 20.56 , 30.00 , 40.3 , 50.4 ])
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
import torch
a = torch.FloatTensor([ 10 , 20 , 30 , 40 , 50 , 1 , 2 , 3 , 4 , 5 ])
print (a.view( 5 , 2 ))
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
import torch
a = torch.FloatTensor([ 10 , 20 , 30 , 40 , 50 ])
print (a.dtype)
print (a[ 0 : 3 ])
print (a[ 4 :])
|
Output:
torch.float32
tensor([10., 20., 30.])
tensor([50.])