PyTorch Add Dimension [With 6 Examples]

In this Python tutorial, we will learn about the PyTorch Add Dimension in Python. Adding a dimension to a tensor is an important part when we are making a machine learning model. For adding a dimension we are using the unsqueeze() method.

And we will also cover different examples related to PyTorch Add Dimension. And we will cover these topics.

  • PyTorch add dimension
  • PyTorch add dimension of size 1
  • PyTorch append dimension
  • PyTorch add batch dimension
  • PyTorch add multiple dimension
  • PyTorch add dimension to tensor

PyTorch Add Dimension

In this section, we will learn about the PyTorch add dimension in Python.

Adding a dimension is an important part when we are making a machine learning model. For adding a dimension we are using the unsqueeze() method.

The unsqueeze() function returns a new tensor with a dimension of size one inserted at the defined position.

Code:

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

  • a = torch.tensor([2, 3, 4, 5]: Here we are describing a variable by using torch.tensor() function.
  • m=torch.unsqueeze(a, 0): Here we are using unsqueeze() method to add dimension.
  • print(m) is used to print the output with the help of the print() function.
# Import library
import torch

# Declare a variable
a = torch.tensor([2, 3, 4, 5])
# Using unsqueeze() method to add dimension
m=torch.unsqueeze(a, 0)
# Print output
print(m)

Output:

After running the above code we get the following output in which we can see that after adding a dimension the tensor values are printed on the screen.

PyTorch add dimension
PyTorch add dimension

So, with this, we understood How we can add dimension in python.

Read: Jax Vs PyTorch

PyTorch Add Dimension Of Size 1

In this section, we will learn about the PyTorch add dimension of size 1 in python.

The unsqueeze approach is used to add the dimension of size 1. Here we put the new dimension in the end, dim = 1 this is how we can identify where the new axis should go.

READ:  How to Fix Module ‘TensorFlow’ has no attribute ‘session’

Code:

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

  • d = torch.tensor([2, 3, 4, 5]): Here we are declaring a variable by using the torch.tensor() function.
  • m = torch.unsqueeze(d,dim= 1): Here we are using the unsqueeze() function to add dimension of size 1.
  • print(m) is used to print the output with the help of the print() function.
# Import library
import torch

# Declare a variable
d = torch.tensor([2, 3, 4, 5])
# Using unsqueeze() method to add dimension
m = torch.unsqueeze(d,dim= 1)
# Print output
print(m)

Output:

After running the above code, we get the following output in which we can see that by adding the dimension of size one we get the new axis.

PyTorch add dimension of size 1
PyTorch add dimension of size 1

So, with this, we understood about the PyTorch add dimension of size 1.

Read: PyTorch Numpy to Tensor

PyTorch Append Dimension

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

Here we appended the dimension by using unsqueeze() method. The unsqueeze() function returns a new tensor with a dimension of size one inserted at the defined position.

Here we put the new dimension in the end, dim = 0 this is how we can identify where the new axis should go.

Code:

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

  • d = torch.Tensor([[3,4], [2,1]]): Here we are creating two dimensional tensor by using torch.tensor() function.
  • print(d.shape) is used to display the shape with the help of the print() function.
  • append = d.unsqueeze(0) is used to append dimension at 0 position.
  • print(append.shape) is used to print the output by using the print() function.
# Import library
import torch
 
# Creating the two dimensional tensor
d = torch.Tensor([[3,4], [2,1]]) 
 
# Showing the shape
print(d.shape)
 
# Append dimension at 0 position
append = d.unsqueeze(0)

# Print output
print(append.shape)

Output:

After running the above code we get the following output in which we can see that after appending the dimension the tensor can move to the new axis.

PyTorch append dimension
PyTorch append dimension

So, with this, we understood how to append dimensions in python.

Read: PyTorch fully connected layer

PyTorch Add Batch Dimension

In this section, we will learn about the PyTorch add batch dimension in python.

READ:  How to Convert Dict to String Python

The PyTorch add batch dimension is defined as a process where we added the dimension in batches.

Here we appended the dimension by using unsqueeze() method. The unsqueeze() function returns a new tensor with a dimension of size one inserted at the defined position.

Code:

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

  • ten = torch.tensor([2,4,6]): Here we are describing the variable by using torch.tensor() function.
  • n= ten.unsqueeze(1) Here we are using unsqueeze() method for add batvh dimension.
  • print(n) is used to print the output by using the print() function.
# Import library
import torch

# Describe varaible
ten = torch.tensor([2,4,6])

# Using unsqueeze() method for add batch dimension
n= ten.unsqueeze(1)

# Print output
print(n)

Output:

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

PyTorch add batch dimension
PyTorch add batch dimension

So, with this, we understood about the PyTorch add batch dimension in python.

Read: PyTorch MNIST Tutorial

PyTorch Add Multiple Dimension

In this section, we will learn about the PyTorch add multiple dimensions in python.

Here we can add multiple dimensions. For adding a dimension we are using the unsqueeze() method.

The unsqueeze is used to insert a new dimension at the given dim and returns the tensor.

Code:

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

  • d = torch.Tensor([2, 4, 6, 8]): Here we are creating one tensor by using torch.tensor() function.
  • print(d.shape) Here we are displaying the shape by using the print() function.
  • add = d.unsqueeze(0) is used to add the dimension at 0 position.
  • add = d.unsqueeze(1) is used to add the dimension at 1 position.
  • print(add.shape) is used to print the output by using print() function.
# importing torch module
import torch
 
# Creating a one dimensional tensor
d = torch.Tensor([2, 4, 6, 8]) 
 
# Showing shape
print(d.shape)
 
# Adding dimension at Zero position
add = d.unsqueeze(0)
 
print(add.shape)
 
# Adding dimension at One position
add = d.unsqueeze(1)

# Print output
print(add.shape)

Output:

After running the above code, we get the following output in which we can see that the PyTorch add multiple dimension values is printed on the screen.

PyTorch add multiple dimension
PyTorch add multiple dimension

This is how we can add the multiple dimension in PyTorch.

READ:  Matplotlib Plot NumPy Array

Read: PyTorch Model Summary

PyTorch Add Dimension To Tensor

In this section, we will learn about the PyTorch add dimension to tensors in python.

Adding a dimension to a tensor is the main part of constructing a machine learning model.

For adding a dimension to a tensor we are using a unsqueeze() function. The unsqueeze() function returns a new tensor with a dimension of size put at the defined position.

Code:

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

  • d = torch.randn(4, 8): Here we are describing a variable by using torch.tensor() function.
  • d = torch.unsqueeze(d, dim=-1): Here we are using unsqeeze() method for adding dimension to tensor.
  • d.shape is used to display the shape.
# Import library
import torch
# Describe a variable 
d = torch.randn(4, 8)
# Using unsqueeze() method
d = torch.unsqueeze(d, dim=-1)
# display shape
d.shape

Output:

After running the above code, we get the following output in which we can see that the PyTorch add dimension value is printed on the screen.

PyTorch add dimension to tensor
PyTorch add dimension to tensor

This is how we can add the dimension to the tensor in PyTorch.

Also, take a look at some more Python PyTorch tutorials.

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

  • PyTorch add dimension
  • PyTorch add dimension of size 1
  • PyTorch append dimension
  • PyTorch add batch dimension
  • PyTorch add multiple dimension
  • PyTorch add dimension to tensor