Creating Tensor in PyTorch

There are various methods to create a tensor in PyTorch.  A tensor can contain elements of a single data type. We can create a tensor using a python list or NumPy array. The torch has 10 variants of tensors for both GPU and CPU. Below are different ways of defining a tensor.

  • torch.Tensor() : It copies the data and creates its tensor. It is an alias for torch.FloatTensor.
  • torch.tensor() : It also copies the data to create a tensor; however, it infers the data type automatically.
  • torch.as_tensor() : The data is shared and not copied in this case while creating the data and accepts any type of array for tensor creation.
  • torch.from_numpy() : It is similar to tensor.as_tensor() however it accepts only numpy array.

Example 2:

Python3




# importing torch module
import torch
import numpy as np
 
# list of values to be stored as tensor
data1 = [1, 2, 3, 4, 5, 6]
data2 = np.array([1.5, 3.4, 6.8,
                9.3, 7.0, 2.8])
 
# creating tensors and printing
t1 = torch.tensor(data1)
t2 = torch.Tensor(data1)
t3 = torch.as_tensor(data2)
t4 = torch.from_numpy(data2)
 
print("Tensor: ",t1, "Data type: ", t1.dtype,"\n")
print("Tensor: ",t2, "Data type: ", t2.dtype,"\n")
print("Tensor: ",t3, "Data type: ", t3.dtype,"\n")
print("Tensor: ",t4, "Data type: ", t4.dtype,"\n")


 Output:

Tensor:  tensor([1, 2, 3, 4, 5, 6]) Data type:  torch.int64 

Tensor: tensor([1., 2., 3., 4., 5., 6.]) Data type: torch.float32

Tensor: tensor([1.5000, 3.4000, 6.8000, 9.3000, 7.0000, 2.8000], dtype=torch.float64) Data type: torch.float64

Tensor: tensor([1.5000, 3.4000, 6.8000, 9.3000, 7.0000, 2.8000], dtype=torch.float64) Data type: torch.float64

What is PyTorch ?

Deep Learning is a branch of Machine Learning where algorithms are written that mimic the functioning of a human brain. The most commonly used libraries in deep learning are Tensorflow and PyTorch. Pytorch is an open-source deep learning framework available with a Python and C++ interface. The PyTorch resides inside the torch module. In PyTorch, the data that has to be processed is input in the form of a tensor.

Similar Reads

How to Install Pytorch?

If you have Anaconda Python Package manager installed in your system, then using by running the following command in the terminal will install PyTorch:...

How PyTorch Works?

Here is an overview of How PyTorch Works –...

PyTorch tensors

...

Creating Tensor in PyTorch

...

Restructuring Tensors in Pytorch

...

Mathematical Operations on Tensors in PyTorch

...

Pytorch Modules

The Pytorch is used to process the tensors. Tensors are multidimensional arrays like n-dimensional NumPy array. However, tensors can be used in GPUs as well, which is not in the case of NumPy array. PyTorch accelerates the scientific computation of tensors as it has various inbuilt functions....

PyTorch Dataset and Dataloader

...

Building Neural Network with PyTorch

There are various methods to create a tensor in PyTorch.  A tensor can contain elements of a single data type. We can create a tensor using a python list or NumPy array. The torch has 10 variants of tensors for both GPU and CPU. Below are different ways of defining a tensor....