How to use PyTorch Full() Function

The PyTorch torch.full() function is defined as that creates a tensor of size filled with fill_value. In detail, we will discuss the full() function using PyTorch in Python.

And additionally, we will cover different examples related to the PyTorch full() function. And we will cover these topics.

  • What is PyTorch full() function
  • PyTorch full() function example
  • What is PyTorch torch.new_full() function
  • What is PyTorch torch.full_like() function
  • How to use PyTorch full() function append

What is PyTorch full() function

In this section, we will learn about the PyTorch full() function in python.

The PyTorch torch.full() function is defined as that creates a tensor of size filled with fill_value and the tensor dtype is deduced from fill_value.

Syntax:

Syntax of the PyTorch full() function is:

torch.full(size, fill_value, out=None, dtype=None, layout=torch.strided, device=None, required_grad=False)

Parameters:

size: The size is a parameter that defines a list, tuple, or size of integers defining the shape of the output tensor.

  • fill_value: The fill value is used to fill the output tensor.
  • out=None: The out is a parameter that is described as an output tensor.
  • dtype=None: The type is defined as the desired datatype of returned tensor. The default value of dtype is None.
  • layout=torch.strided is defined as the desired layout of returned tensor and the default value of layout is a torch.strided.
  • device=None is defined as the desired device of the returned tensor. The default value of the device is None.
  • requires_grad: The requires_grad is defined as a parameter if autograd should record operations on the returned tensor and the default value of requires_grad is False.

So, with this, we understood PyTorch’s full function with the help of a torch.full() method.

Read: PyTorch Conv3d

PyTorch full() function example

In this section, we will learn how to implement the full() function with the help of an example in python.

Here we are using the torch.full() that creates a tensor of size size filled with fill_value.

Code:

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

  • f = torch.full((4, 6), 3.141592): Here we are calling the full() function and storing the result in the f variable.
  • print(“The full() tensor value of f:”, f) is used to print the full() function value of f by using print() function.
  • g = torch.full((3, 5), 3.1) is used to describe a full() function and storing the result in the g variable.
# Importing the torch library
import torch
# Calling the full() function and storing the result tensor in variable f
f = torch.full((4, 6), 3.141592)
# Print the full function tensor 
print("The full() function value of f:", f)
# Calling the full() function and storing the result tensor in variable g
g = torch.full((3, 5), 3.1)
# Print the full function tensor 
print("The full() function value of g:", g)

Output:

After running the above code, we get the following output in which we can see that the PyTorch full() function values are printed on the screen.

PyTorch full function example
PyTorch full function example

This is how we understand the PyTorch full() function with the help of an example in python.

Read: PyTorch View Tutorial

What is PyTorch torch.new_full() function

In this section, we will learn about the PyTorch torch.new_full() function in python.

The PyTorch torch.new_full() function returns the tensor of size size that filled with fill_value and by default, as this device returned values has the same torch.dtype and torch.device.

Syntax:

Syntax of the PyTorch torch.new_full() function:

torch.new_full(size, fill_value, dtype=None, device=None, requires_grad=False)

Parameters:

fill_value: The fill_value is a parameter that describes the numbers to fill the output tensor.

  • dtype: The type is defined as the desired type of returned tensor and the default value of dtype is None.
  • device: A device is defined as the desired type of returned tensor. The default value of the device is None.
  • requires_grad: The requires_grad is defined as a parameter if autograd should record operations on the returned tensor and the default value of requires_grad is False.

Example:

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

  • ten = torch.ones((4,), dtype=torch.float64): Here we are describing the tensor variable by using the torch.ones() function.
  • a= ten.new_full((5, 6), 3.382792): Here we are calling the tensor.new_full() function.
  • print(a) is used to print the torch.new_full() function values on the screen by using print() function.
# Importing the torch library
import torch
# Describing the variable
ten = torch.ones((4,), dtype=torch.float64)
# Calling the tensor.new_full()
a= ten.new_full((5, 6), 3.382792)
# Print the torch.new_full() value
print(a)

Output:

After running the above code we get the following output in which we can see that the PyTorch torch.new_full() function values are printed on the screen.

PyTorch torch.new_full function
PyTorch torch.new_full function

This is how we understand the PyTorch torch.new_full() function in python.

Read: PyTorch Add Dimension

What is PyTorch torch.full_like() function

In this section, we will learn about the PyTorch torch.full_like() function in python.

The PyTorch torch.full_like() function that returns a tensor with the same size as input filled with fill_value.

Syntax:

Syntax of the PyTorch torch.full_like() function:

torch.full_like(input, fill_value, dtype=None, layout=torch.strided, device=None, required_grad=False, memory_format=torch.preserve_format)

Parameters:

The following are the parameters of the PyTorch torch.full_like() function:

  • input: The input is the parameter in which the size of input will determine the size of the output tensor.
  • fill_value: The fill_value is defined as the number to fill the output tensor.
  • dtype: The dtype is defined as the desired datatype of the returned tensor and the default value of the dtype is None.
  • layout: The layout is a parameter of that desired layout of the returned tensor. The default value of the layout is None.
  • device: The device is defined as the desired device of the returned tensor and the default value is None.
  • requires_grad: The requires_grad is defined as a parameter if autograd should record operations on the returned tensor and the default value of requires_grad 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.preserve_format.

So, with this, we understood the PyTorch torch.full_like() function in python.

Read: Jax Vs PyTorch

How to use PyTorch full() function append

In this section, we will learn about how to use the PyTorch full() function append in python.

Before moving forward we should have a piece of knowledge about append.

The append is defined as a process to add something to the end of the written document.

Here we append the integer in the PyTorch full function by using the torch.full() method.

Code:

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

  • f = torch.full([5, 6], 4.55) is used to apply the full() function and store the resulting tensor in f.
  • print(“f = “, f) is used to print the value of f by using the print() function.
  • g = torch.full([4, 7], 2.5) is used to apply the full() function and store the resulting tensor in g.
  • print(“g = “, g) is used to print the value g with the help of the print() function.
# Importing the torch library
import torch
    
# Applying the full function and storing the resulting tensor in 'f'
f = torch.full([5, 6], 4.55)
# Print the variable f
print("f = ", f)
# Applying the full function and storing the resulting tensor in 'g'
g = torch.full([4, 7], 2.5)
# Print the variable g
print("g = ", g)

Output:

After running the above code, we get the following output in which we can see that the PyTorch full() function append values are printed on the screen.

PyTorch full function append
PyTorch full function append

This is how we understand how to use the PyTorch full() function append.

You may also like to read the following PyTorch tutorials.

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

  • What is PyTorch full() function
  • PyTorch full() function example
  • What is PyTorch torch.new_full() function
  • What is PyTorch torch.full_like() function
  • How to use PyTorch full() function append