torch.unsqueeze¶
- torch.unsqueeze(input, dim) Tensor ¶
傳回一個新的張量,在指定位置插入大小為 1 的維度。
傳回的張量與此張量共享相同的底層資料。
可以使用範圍在
[-input.dim() - 1, input.dim() + 1)
內的dim
值。負數的dim
對應到在dim
=dim + input.dim() + 1
套用unsqueeze()
。範例
>>> 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]])