Open In App

One-Dimensional Tensor in Pytorch

In this article, we are going to discuss a one-dimensional tensor in Python. We will look into the following concepts:

  1. Creation of One-Dimensional Tensors
  2. Accessing Elements of Tensor
  3. Size of Tensor
  4. Data Types of Elements of Tensors
  5. View of Tensor
  6. 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




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

Example: Python program to access elements using the index.




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




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




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




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

Example: Python program to create a tensor with 10 elements and view with 5 rows and 2 columns and vice versa.




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




# 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.])

 

 


Article Tags :