79622046

Date: 2025-05-14 18:11:33
Score: 0.5
Natty:
Report link

In PyTorch, unsqueeze() is a tensor operation that adds a dimension of size one at a specified position (axis) in the tensor's shape, effectively increasing its dimensionality without changing its data. This is useful when you need to align tensor shapes for broadcasting or model input requirements. For example, if x is a 1D tensor with shape [4], torch.unsqueeze(x, 0) transforms it into a 2D tensor with shape [1, 4], and torch.unsqueeze(x, 1) transforms it into shape [4, 1]. It’s commonly used in scenarios like adding a batch dimension or a channel dimension in machine learning workflows.

Reasons:
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Single line (0.5):
  • Low reputation (1):
Posted by: Manas Parashar