One-Dimensional Tensor in Pytorch - GeeksforGeeks

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 TensorsAccessing Elements of TensorSize of TensorData Types of Elements of TensorsView of TensorFloating Point Tensor
Creation of One-Dimensional Tensors
Accessing Elements of Tensor
Size of Tensor
Data Types of Elements of Tensors
View of Tensor
Floating Point Tensor
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.
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
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 moduleimport torch  # create one dimensional tensor with integer type elementsa = torch.tensor([10, 20, 30, 40, 50])print(a)  # create one dimensional tensor with float type elementsb = 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])
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 moduleimport torch  # create one dimensional tensor with integer type elementsa = torch.tensor([10, 20, 30, 40, 50])  # get 0 and 1 index elementsprint(a[0], a[1])  # get 4 th index  elementprint(a[4])  # get 4 index element from lastprint(a[-4])  # get 2 index element from lastprint(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 moduleimport torch  # create one dimensional tensor with integer type elementsa = torch.tensor([10, 20, 30, 40, 50])  # access elements from 1 to 4print(a[1:4])  # access from 4print(a[4:])  # access from lastprint(a[-1:])
Output:
tensor([20, 30, 40])
tensor([50])
tensor([50])
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 moduleimport torch  # create one dimensional tensor integer type elementsa = torch.FloatTensor([10, 20, 30, 40, 50])  # size of tensorprint(a.size())  # create one dimensional tensor integer type elementsb = torch.FloatTensor([10, 20, 30, 40, 50, 45, 67, 43])  # size of tensorprint(b.size())
Output:
torch.Size([5])
torch.Size([8])
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 moduleimport torch  # create one dimensional tensor with integer type elementsa = torch.tensor([10, 20, 30, 40, 50])  # get data type of vector aprint(a.dtype)  # create one dimensional tensor with float type elementsb = torch.tensor([10.12, 20.56, 30.00, 40.3, 50.4])  # get data type of vector bprint(b.dtype)
Output:
torch.int64
torch.float32
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 moduleimport torch  # create one dimensional tensor 10 elementsa = torch.FloatTensor([10, 20, 30, 40, 50, 1, 2, 3, 4, 5])  # view tensor in 5 rows and 2 columnsprint(a.view(5, 2))  # view tensor in 2 rows and 5 columnsprint(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.]])
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 moduleimport torch  # create one dimensional Float Tensor  with # integer type elementsa = torch.FloatTensor([10, 20, 30, 40, 50])  # display data typeprint(a.dtype)  # access elements from 0 to 3print(a[0:3])  # access from 4print(a[4:])
Output:
torch.float32
tensor([10., 20., 30.])
tensor([50.])
Picked
Python-PyTorch
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Install PIP on Windows ?
How to drop one or multiple columns in Pandas Dataframe
How To Convert Python Dictionary To JSON?
Check if element exists in list in Python
Defaultdict in Python
Python | Get unique values from a list
Python | os.path.join() method
Selecting rows in pandas DataFrame based on conditions
Create a directory in Python
Python | Pandas dataframe.groupby()