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

分类目录:《深入浅出Pytorch函数》总目录

相关文章:

· 深入浅出Pytorch函数------torch.squeeze

· 深入浅出Pytorch函数------torch.unsqueeze


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

语法

c 复制代码
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]])
相关推荐
whaosoft-1431 小时前
51c自动驾驶~合集7
人工智能
刘晓倩4 小时前
Coze智能体开发实战-多Agent综合实战
人工智能·coze
石迹耿千秋5 小时前
迁移学习--基于torchvision中VGG16模型的实战
人工智能·pytorch·机器学习·迁移学习
路人蛃8 小时前
通过国内扣子(Coze)搭建智能体并接入discord机器人
人工智能·python·ubuntu·ai·aigc·个人开发
CV-杨帆8 小时前
论文阅读:arxiv 2025 A Survey of Large Language Model Agents for Question Answering
论文阅读·人工智能·语言模型
绝顶大聪明8 小时前
【深度学习】神经网络-part2
人工智能·深度学习·神经网络
加百力9 小时前
AI助手竞争白热化,微软Copilot面临ChatGPT的9亿下载挑战
人工智能·microsoft·copilot
Danceful_YJ9 小时前
16.使用ResNet网络进行Fashion-Mnist分类
人工智能·深度学习·神经网络·resnet
香蕉可乐荷包蛋10 小时前
AI算法之图像识别与分类
人工智能·学习·算法
张较瘦_10 小时前
[论文阅读] 人工智能 + 软件工程 | 当LLMs遇上顺序API调用:StateGen与StateEval如何破解测试难题?
论文阅读·人工智能