深入浅出Pytorch函数——torch.unsqueeze

分类目录:《深入浅出Pytorch函数》总目录
相关文章:
· 深入浅出Pytorch函数——torch.squeeze
· 深入浅出Pytorch函数——torch.unsqueeze


返回一个新的张量,且在指定位置新插入了一个大小为1的维度,返回的张量与该张量共享相同的底层数据。

语法

torch.unsqueeze(input, dim) → Tensor

参数

  • input:[Tensor] 输入张量
  • dim:[int] 插入维度的位置索引,范围是 [ -input.dim() − 1 , input.dim() + 1 ] [\text{-input.dim()} - 1, \text{input.dim()} + 1] [-input.dim()1,input.dim()+1],默认值为 input.dim() + 1 \text{input.dim()} + 1 input.dim()+1

实例

输入:

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

输出:

tensor([[ 1,  2,  3,  4]])

输入:

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

输出:

tensor([[1],
        [2],
        [3],
        [4]])

猜你喜欢

转载自blog.csdn.net/hy592070616/article/details/131872525