原型
torch.unsqueeze(input, dim, out=None) → Tensor
功能
在指定的位置增加一个维度
参数
input (Tensor) – 要增加维度的张量
dim (int) – 在哪个维度前,范围是[-input.dim() - 1, input.dim() + 1)]
out (Tensor, optional) – 可选 输出的张量.
例子
>>> x = torch.tensor([1,2,3,4])
>>> x
tensor([1, 2, 3, 4])
>>> torch.unsqueeze(x,0)
tensor([[1, 2, 3, 4]])
x本来是(4)在第零维度上加上一维度(1,4)
再如
>>> x
tensor([1, 2, 3, 4])
>>> torch.unsqueeze(x,1)
tensor([[1],
[2],
[3],
[4]])
(4) 变为(4,1)