評價此頁

torch.unsqueeze#

torch.unsqueeze(input, dim) Tensor#

在指定位置插入一個大小為一的新維度,並返回一個新的張量。

返回的張量與此張量共享相同的底層資料。

[-input.dim() - 1, input.dim() + 1) 範圍內的 dim 值均可使用。負數 dim 將對應於在 dim = dim + input.dim() + 1 處應用 unsqueeze()

引數
  • input (Tensor) – 輸入張量。

  • dim (int) – 插入單例維度的索引

示例

>>> 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]])