Torch官网解释

torch.unsqueeze(inputdim) → Tensor

        Returns a new tensor with a dimension of size one inserted at the specified position.

        The returned tensor shares the same underlying data with this tensor

函数的功能是将输入增加一个维度,dim决定在哪一层增加一个维度

>>> x = torch.tensor([1, 2, 3, 4])
>>> torch.unsqueeze(x, 0)
tensor([[ 1,  2,  3,  4]])
>>> torch.unsqueeze(x, 1)
tensor([[ 1],
        [ 2],
        [ 3],
        [ 4]])

以下是Stack Overflow里的样例图

Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐