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