Use torch.unsqueeze(input, dim, out=None):

>>> import torch
>>> a = torch.Tensor([1, 2, 3, 4, 5])
>>> a

 1
 2
 3
 4
 5
[torch.FloatTensor of size 5]

>>> a = a.unsqueeze(0)
>>> a

 1  2  3  4  5
[torch.FloatTensor of size 1x5]
Answer from Haha TTpro on Stack Overflow
๐ŸŒ
Codefinity
codefinity.com โ€บ courses โ€บ v2 โ€บ 1dd2b0f6-6ec0-40e6-a570-ed0ac2209666 โ€บ 8a329f24-2e0c-4e15-bc32-802e817f6856 โ€บ 6f054f4c-f699-45fb-a1d4-0d252768f49c
Learn Shapes and Dimensions in PyTorch | PyTorch Introduction
1234567 import torch tensor = torch.arange(12) view_tensor = tensor.view(2, 6) view_tensor[0, 0] = 999 # Changes in the view are reflected in the original tensor print("View Tensor:", view_tensor) print("Original Tensor:", tensor) ... 12345678 import torch tensor = torch.arange(12) # Add a new dimension unsqueezed_tensor = tensor.unsqueeze(0) # Add a batch dimension print(f"Unsqueezed tensor: {unsqueezed_tensor.shape}") # Remove a dimension of size 1 squeezed_tensor = unsqueezed_tensor.squeeze(0) print(f"Squeezed Tensor: {squeezed_tensor.shape}")
๐ŸŒ
Python Guides
pythonguides.com โ€บ pytorch-add-dimension
How to Add Dimension in PyTorch - Python Guides
June 18, 2025 - This method is particularly handy ... on the fly. ... Another approach is to use reshape() or view() to explicitly specify the desired tensor shape, including the new dimension....
๐ŸŒ
Sprintchase
sprintchase.com โ€บ home โ€บ how to use pytorch view() method
How to Use PyTorch view() Method
May 12, 2025 - import torch batch = torch.randn(8, ... print(flatten.shape) # Output: torch.Size([8, 3072]) You can use .unsqueeze() or .view() to add or remove dimensions from the input tensor....
๐ŸŒ
Medium
medium.com โ€บ biased-algorithms โ€บ guide-to-adding-dimensions-to-pytorch-tensors-732513bbff8b
Guide to Adding Dimensions to PyTorch Tensors | by Hey Amit | Biased-Algorithms | Medium
December 5, 2024 - Thereโ€™s more than one way to add dimensions in PyTorch, and each method has its strengths. Hereโ€™s a quick overview: Let me break these down with practical examples. This is my go-to method when I need precision. Hereโ€™s why: unsqueeze() lets you specify exactly where you want the new dimension, making it perfect for tensors with complex shapes. tensor = torch.tensor([1, 2, 3]) # Shape: (3,) # Add a dimension at the beginning expanded_tensor = tensor.unsqueeze(0) # Shape: (1, 3) print(expanded_tensor) # Add a dimension at the end expanded_tensor = tensor.unsqueeze(-1) # Shape: (3, 1) print(expanded_tensor)
๐ŸŒ
Clay-Technology World
clay-atlas.com โ€บ home โ€บ [pytorch] use view() and permute() to change dimension shape
[PyTorch] Use view() and permute() To Change Dimension Shape - Clay-Technology World
August 11, 2021 - # coding: utf-8 import torch inputs ... in the tensor will not change. In addition, view() can not only replace the order of dimensions, but also directly change the dimensions....
๐ŸŒ
Codecademy
codecademy.com โ€บ docs โ€บ pytorch โ€บ tensor operations โ€บ .expand()
PyTorch | Tensor Operations | .expand() | Codecademy
May 16, 2025 - The .expand() function in PyTorch creates a new view of a tensor by expanding its singleton dimensions (dimensions with size 1) to a larger size.
Find elsewhere
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ how-to-resize-a-tensor-in-pytorch
How to resize a tensor in PyTorch? - GeeksforGeeks
March 23, 2022 - We can resize the tensors in PyTorch by using the view() method. view() method allows us to change the dimension of the tensor but always make sure the total number of elements in a tensor must match before and after resizing tensors.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ change-view-of-tensor-in-pytorch
Change view of Tensor in PyTorch - GeeksforGeeks
October 18, 2022 - In the below example, we will see how to get a three or more-dimensional vector using the view function. This function helps a lot while handling image data as they are three-dimensional having RGB channels. Also, when we make a batch of these images it increases to four dimensions. ... # importing torch module import torch import numpy as np # create one dimensional tensor having 18 elements a = torch.FloatTensor(np.arange(18)) # view tensor in 2 x 3 x 3 print(a.view(2, 3, 3))
๐ŸŒ
PyTorch Forums
discuss.pytorch.org โ€บ uncategorized
What is the difference between view() and unsqueeze()? - Uncategorized - PyTorch Forums
March 17, 2017 - Use of unsqueeze(): input = torch.Tensor(2, 4, 3) # input: 2 x 4 x 3 print(input.unsqueeze(0).size()) # prints - torch.size([1, 2, 4, 3]) Use of view(): input = torch.Tensor(2, 4, 3) # input: 2 x 4 x 3 print(input.view(1, -1, -1, -1).size()) # prints - torch.size([1, 2, 4, 3]) According to documentation, unsqueeze() inserts singleton dim at position given as parameter and view() creates a view with different dimensions of the storage associated with tensor.
๐ŸŒ
Medium
medium.com โ€บ @amit25173 โ€บ expert-guide-to-resizing-pytorch-tensors-b88d3f34a76b
Expert Guide to Resizing PyTorch Tensors | by Amit Yadav | Medium
February 9, 2025 - For example, if youโ€™re just adding or removing a dimension, use operations like unsqueeze or squeeze, which donโ€™t duplicate data. ... import torch # Example of memory-efficient resizing x = torch.randn(1000, 1000) # Adding a dimension with unsqueeze (no new data created) x_unsqueezed = x.unsqueeze(0) # Alternative using reshape (may create a new copy if non-contiguous) x_reshaped = x.reshape(1, 1000, 1000) print("Memory addresses are the same:", x.data_ptr() == x_unsqueezed.data_ptr())
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ reshaping-a-tensor-in-pytorch
Reshaping a Tensor in Pytorch - GeeksforGeeks
September 1, 2021 - Example 1: Python program to create a tensor with 12 elements and view with 3 rows and 4 columns and vice versa. ... # importing torch module import torch # create one dimensional tensor 12 elements a=torch.FloatTensor([24, 56, 10, 20, 30, 40, 50, 1, 2, 3, 4, 5]) # view tensor in 4 rows and 3 columns print(a.view(4, 3)) # view tensor in 3 rows and 4 columns print(a.view(3, 4))
๐ŸŒ
PyTorch Forums
discuss.pytorch.org โ€บ vision
Torch.view() dimensions constraint - vision - PyTorch Forums
July 5, 2020 - Hello I am doing some research on CNN and i want to extract manually features through a function. The problem i am facing is that i want to change the dimension of the output tensor of an image batch to treat it in a more lineal way. I am using VGG16 and AlexNet.
๐ŸŒ
GitHub
github.com โ€บ torch โ€บ torch7 โ€บ issues โ€บ 824
Convenience function to add a singleton dimension to a torch tensor. ยท Issue #824 ยท torch/torch7
October 29, 2016 - local numChannels, height, width = image:size(1), roadImage:size(2), roadImage:size(3) image = image:view(1, numChannels, height, width) I would really like a one-liner for this. Does anyone have any suggestions? If not, how open are the maintainers to my adding a new function to the library to do this? ... There is a helper function for that in nn if you want. It can be found in nn.utils.addSingletonDimension. Given that there is a nn function called Unsqueeze that does exactly what you want (and no torch counterpart), I think it's reasonable to include a unsqueeze function in torch following the implementation of addSingletonDimension.
๐ŸŒ
PyTorch
docs.pytorch.org โ€บ reference api โ€บ torch.tensor โ€บ torch.tensor.view
torch.Tensor.view โ€” PyTorch 2.14 documentation
January 1, 2023 - For a tensor to be viewed, the new view size must be compatible with its original size and stride, i.e., each new view dimension must either be a subspace of an original dimension, or only span across original dimensions ... Otherwise, it will not be possible to view self tensor as shape without copying it (e.g., via contiguous()). When it is unclear whether a view() can be performed, it is advisable to use reshape(), which returns a view if the shapes are compatible, and copies (equivalent to calling contiguous()) otherwise. ... >>> x = torch.randn(4, 4) >>> x.size() torch.Size([4, 4]) >>> y