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