a = torch.zeros(4, 5, 6)
a = a[:, :, None, :]
assert a.shape == (4, 5, 1, 6)
Answer from Gulzar on Stack Overflow Top answer 1 of 3
74
a = torch.zeros(4, 5, 6)
a = a[:, :, None, :]
assert a.shape == (4, 5, 1, 6)
2 of 3
49
You can add a new axis with torch.unsqueeze() (first argument being the index of the new axis):
>>> a = torch.zeros(4, 5, 6)
>>> a = a.unsqueeze(2)
>>> a.shape
torch.Size([4, 5, 1, 6])
Or using the in-place version: torch.unsqueeze_():
>>> a = torch.zeros(4, 5, 6)
>>> a.unsqueeze_(2)
>>> a.shape
torch.Size([4, 5, 1, 6])
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 - Here’s a trick that feels almost too simple to be true: You can use Python’s native indexing or slicing to add a new dimension to your tensor. This method shines when you’re working in dynamic loops or writing quick, one-off operations. ... # Original tensor tensor = torch.tensor([1, 2, 3]) print("Original tensor shape:", tensor.shape) # Shape: (3,) # Add a new dimension using slicing sliced_tensor = tensor[None, :] # Add at the start print("Shape after slicing:", sliced_tensor.shape) # Shape: (1, 3)
YouTube
youtube.com › tsinfo technologies
How to Add Dimension in Python PyTorch | Add Dimension in PyTorch - YouTube
In this Python PyTorch Video tutorial, I will understand how to add dimension using PyTorch. Here, I have shown how to add dimension in PyTorch.Adding a dime...
Published: October 15, 2022
Views: 175
YouTube
youtube.com › data science weekly
Add A New Dimension To The End Of A Tensor In PyTorch - YouTube
Add a new dimension to the end of a PyTorch tensor by using None-style indexing. PyTorch Tutorial Video.
Published: March 8, 2022
Views: 49
Top answer 1 of 11
56
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]
2 of 11
23
you might use
a.view(1,5)
Out:
1 2 3 4 5
[torch.FloatTensor of size 1x5]
YouTube
youtube.com › watch
Add A New Dimension To The Beginning Of A Tensor In PyTorch - YouTube
Add a new dimension to the beginning of a PyTorch tensor by using None-style indexing. PyTorch Tutorial
Published: March 8, 2022
GeeksforGeeks
geeksforgeeks.org › deep learning › repeating-tensors-in-a-specific-new-dimension-in-pytorch
Repeating Tensors in a Specific New Dimension in PyTorch - GeeksforGeeks
July 23, 2025 - For example, a 1D tensor of size (3) can be transformed into a 2D tensor of size (3, 5) by repeating the original tensor's values along a new dimension. torch.repeat() function generates a new tensor same like the original tensor but repeat the elements along the specified dimensions.
Data Science Weekly
datascienceweekly.org › tutorials › add-a-new-dimension-to-the-end-of-a-tensor-in-pytorch
Add A New Dimension To The End Of A Tensor In PyTorch
Let’s check the dimensions by using the PyTorch size operation to see if PyTorch did create a new axis for us at the end of the tensor. ... Bingo! We see that we now have a new axis at the end. Before, it was 2x4x6x8. Now, it’s 2x4x6x8x1. Perfect - We were able to add a new dimension to the end of a PyTorch tensor by using None style indexing.
Jake Tae
jaketae.github.io › study › pytorch-tensor
PyTorch Tensor Basics - Jake Tae
October 10, 2020 - The conclusion of this analysis is clear: use torch.tensor() instead of torch.Tensor(). Indeed, this SO post also confirms the fact that torch.tensor() should generally be used, as torch.Tensor() is more of a super class from which other classes inherit. As it is an abstract super class, using it directly does not seem to make much sense. In PyTorch, there are two ways of checking the dimension of a tensor: .size() and .shape.
PyTorch Forums
discuss.pytorch.org › uncategorized
Dynamically add dimensions for broadcasting of a vector to a tensor - Uncategorized - PyTorch Forums
March 21, 2022 - Imagine I have this vector: import torch x = torch.rand(10) and I want to multiply it by this tensor: y = torch.rand(10, 20, 30, 3) I can’t do x * y directly. I can’t do x.expand_as(y) * y either, expand can apparently only stack dims on top of the current dim, and there will be a mismatch.