Create PyTorch Empty Tensor

The tensor.empty() function returns the tensor that is filled with uninitialized data. The tensor shape is defined by the variable argument called size. In detail, we will discuss Empty Tensor using PyTorch in Python.

And additionally, we will cover different examples related to the PyTorch Empty Tensor. And we will cover these topics.

  • What is PyTorch empty tensor
  • PyTorch empty tensor example
  • How to create PyTorch empty tensor append
  • PyTorch empty tensor check
  • How to create PyTorch empty tensor using concate
  • How to create PyTorch empty tensor list

PyTorch empty tensor

In this section, we will learn about the PyTorch empty tensor in python.

The tensor.empty() function returns the tensor that is filled with uninitialized data and the tensor shape is defined by the variable argument called size.

Syntax:

Syntax of the PyTorch empty tensor is :

torch.empty(size, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False, pin_memory=False, memory_format=torch.contiguous_format)

Parameters:

The following are the parameters of the PyTorch empty tensor:

  • size: Size is a sequence of integers that are defining the shape of the output tensor.
  • out: The out is a parameter that defines the output tensor.
  • dtype: The type is the desired datatype of the returned tensor and the default value of dtype is None.
  • layout: The layout is defined as the desired layout of returned tensor. The default value of the layout is torch.strided.
  • device: The device is defined as the desired device of returned tensor and the default value of the device is None.
  • requires_grad: The requires_grad is a parameter. If the autograd should record operation on the returned tensor. The default value of requires_grad is False.
  • pin_memory: The pin_memory is a parameter that defines if a set, returned tensor would be allocated in the pinned memory and works only for CPU tensors and the default value of the pin_memory is False.
  • memory_format: The memory_format is defined as the desired memory format of the returned tensor and the default value of the memory_format is a torch.contiguous_format.

This is how we understand about the PyTorch empty tensor with the help of the torch.empty() function.

Read: How to use PyTorch Full() Function

PyTorch empty tensor example

In this section, we will learn about how we can implement the Pytorch empty tensor with the help of an example in python.

In the PyTorch empty tensor example, we are using the torch.empty() that can return the tensor pack with the close data, and the shape of the tensor is defined by the variable argument called size.

Code:

In the following code, we will import the torch library as import torch.

  • e=torch.empty((3,4), dtype=torch.int64): Here we are describe the e variable and calling the empty() function.
  • f= torch.empty((5,6), dtype=torch.int64): Here we are calling the empty() function.
  • print(“The empty tensor value e :”, e) is used to print the empty tensor e value with the help of the print() function.
# Import library
import torch
# Calling the empty() function
e=torch.empty((3,4), dtype=torch.int64)
f= torch.empty((5,6), dtype=torch.int64)
# Print the empty tensor values
print("The empty tensor value e :", e)
print("The empty tensor value f :", f)

Output:

After running the above code we get the following output in which we can see that the PyTorch empty tensor values are printed on the screen.

Pytorch empty tensor example
Pytorch empty tensor example

So, with this, we understood about the Pytorch empty tensor with the help of an example of using torch.empty() function.

Read: PyTorch Flatten + 8 Examples

PyTorch empty tensor append

In this section, we will learn about the PyTorch empty tensor append in python.

  • Before moving forward we should have a piece of knowledge about the append.
  • Append is defined as an operation that can add something to the end of a written document.
  • Here we are appending the empty tensor that can return the tensor pack with the close data.

Code:

In the following code, we will import the torch module as import torch.

  • a= torch.empty(2): Here we are calling the torch.empty() function by using variable a.
  • print(“a”, a) is used to print the value of a by using the print() function.
  • b= torch.empty(4): Here we are calling the torch.empty() function by using variable b.
# Import torch
import torch
# Calling the empty() function a
a= torch.empty(2)
print("a", a)
torch.Tensor([0.])

# Calling the empty() function b
b= torch.empty(4)
print("b", b) 
torch.Tensor([0., 0., 0.])
 
# Calling the empty() function c
c= torch.empty(4,5)
print("c", c) 

Output:

After running the above code, we get the following output in which we can see that the PyTorch empty tensor values are printed on the screen.

PyTorch empty tensor append
PyTorch empty tensor append

So, with this, we understood the PyTorch empty tensor append with the help of a torch.empty() function.

Read: PyTorch Conv3d – Detailed Guide

PyTorch empty tensor check

In this section, we will learn about the PyTorch empty tensor check in python.

A check is to study or test something in sequence to make confident that it is safe or right and in good condition.

Code:

In the following code, we will import the torch library for checking the empty tensor in python.

  • f = torch.empty([5, 6]): Here we are calling the empty function and storing the resulting tensor in f.
  • print(“f = “, f) is used to print the values of f by using the print() function.
  • g = torch.empty([4, 5]): Here we are calling the empty() function and storing the resulting tensor in g.
# Importing the torch library
import torch
  
  
# Calling the empty function and storing the resulting tensor in 'f'
f = torch.empty([5, 6])
print("f = ", f)

# Calling the empty function and storing the resulting tensor in 'g'
g = torch.empty([4, 5])
print("g = ", g)

Output:

After running the above code, we get the following output in which we can see that the PyTorch empty tensor check values are printed on the screen.

PyTorch empty tensor check
PyTorch empty tensor check

This is how we can understand how we can check the PyTorch empty tensor in python.

Read: PyTorch Conv1d

PyTorch empty tensor concate

In this section, we will learn about the PyTorch empty tensor concate in python.

Before moving forward we should have a piece of knowledge about the PyTorch empty tensor concate.

The PyTorch empty tensor concate function is used to concatenate two or more tensors with a row or column by using a torch.cat() function.

Code:

In the following code, we will import the torch library as import torch.

  • b = torch.empty(1,2): Here we are calling the torch.empty() function.
  • b = torch.cat([b, a], dim=0): Here we are calling the torch.cat() function.
  • print(b) is used to print the value of variable b by using the print() function.
# Import library
import torch
# Calling the empty() function
b = torch.empty(1,2)
for x in range(3):
  a = torch.FloatTensor([[2,3],[5,6]])
  # Calling the torch.cat() function
  b = torch.cat([b, a], dim=0)
# Print the value of b
print(b)

Output:

After running the above code, we get the following output in which we can see that the PyTorch empty tensor concate values are printed on the screen.

PyTorch empty tensor concate
PyTorch empty tensor concate

This is how we understood about the PyTorch empty tensor concate with the help of a torch.empty() and torch.cat() function.

Read: PyTorch Hyperparameter Tuning

PyTorch empty tensor list

In this section, we will learn about the PyTorch empty tensor list in python.

The list is defined as a sequence of names and figures that are printed one after the another.

The PyTorch empty tensor list is defined as one that can return the tensor pack with the close data by using a torch.empty() function.

Code:

In the following code, we will import the torch library as import torch.

  • a = torch.empty((5,6), dtype=torch.int64): Here we are calling the torch.empty() function.
  • print(“The value of an empty tensor list of variable a:”, a) is used to print the value of the empty tensor list of variable a with the help of the print() function.
# Import library
import torch
# Calling the empty() function of a
a = torch.empty((5,6), dtype=torch.int64)
# Calling the empty() function of b
b = torch.empty((7,8,9), dtype=torch.int64)
# Print the value if variable a
print("The value of empty tensor list of variable a:", a)
# Print the value if variable b
print("The value of empty tensor list of variable b:", b)

Output:

After running the above code, we get the following output in which we can see that the PyTorch empty tensor list values are printed on the screen.

PyTorch empty tensor list
PyTorch empty tensor list

You may also like to read the following Python PyTorch tutorials.

So, in this tutorial, we discussed PyTorch empty tensor and have also covered different examples related to its implementation. Here is the list of examples that we have covered.

  • What is PyTorch empty tensor
  • PyTorch empty tensor example
  • How to create PyTorch empty tensor append
  • PyTorch empty tensor check
  • How to create PyTorch empty tensor using concate
  • How to create PyTorch empty tensor list