Python Pytorch empty() method

PyTorch is an open-source machine learning library developed by Facebook. It is used for deep neural network and natural language processing purposes.

The function torch.empty() returns a tensor filled with uninitialized data. The shape of the tensor is defined by the variable argument size.

Syntax: torch.empty(size, out=None)

size: a sequence of integers defining the shape of the output tensor
out (Tensor, optional): the output tensor

Return type: A tensor

Code #1:





# Importing the PyTorch library
import torch
# Applying the empty function and
# storing the resulting tensor in 'a'
a = torch.empty([3, 4])
print("a = ", a)
b = torch.empty([2, 3])
print("b = ", b)



a =  tensor([[2.0283e-19, 6.9772e+22, 1.8943e+23, 1.1432e-32],
        [1.3563e-19, 1.8888e+31, 8.9066e-15, 1.8888e+31],
        [6.4639e-04, 6.8608e+22, 1.7753e+28, 2.0535e-19]])
b =  tensor([[0., 0., 0.],
        [0., 0., 0.]])


Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using or mail your article to See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :

Be the First to upvote.

Please write to us at to report any issue with the above content.