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