pytorch学习笔记(1)

 

 

 

expand向左扩展维度、扩展元素个数

a=t.ones(2,3)

只能在左侧增加维度,而不能在右侧增加维度,也不能在中间增加维度

新增维度的元素个数可以为任意数字

 

a2=a.expand(1,2,3)

print(a2.shape)#torch.Size([1, 2, 3])

 

左侧增加一个维度,元素个数为1

a2=a.expand(1,1,2,3)

print(a2.shape)#torch.Size([1, 1, 2, 3])

 

左侧增加1个维度,元素个数1

a2=a.expand(3,2,3)

print(a2.shape)#torch.Size([3, 2, 3])

左侧增加1个维度,元素个数复制为3份

# a2=a.expand(2,3,1)

# print(a2.shape)

# RuntimeError: The expanded size of the tensor (1) must match the existing size (3) at non-singleton dimension 2.  Target sizes: [2, 3, 1].  Tensor sizes: [2, 3]

Error:右侧增加维度,报错

a2=a.expand(2,1,3)

print(a2.shape)

# RuntimeError: The expanded size of the tensor (1) must match the existing size (2) at non-singleton dimension 1.  Target sizes: [2, 1, 3].  Tensor sizes: [2, 3]

Error:中间增加维度,报错

 

view扩展一个或者多个维度

a=t.ones(2,3)

扩展维度上的数字(元素个数)只能是1

a2=a.view(1,2,3)

print(a2.shape)#torch.Size([1, 2, 3])

扩展第0维:[2, 3]→[1, 2, 3]

a2=a.view(2,1,3)

print(a2.shape)#torch.Size([2, 1, 3])

扩展第1维:[2, 3]→[2, 1, 3]

a2=a.view(2,3,1)

print(a2.shape)#torch.Size([2, 3, 1])

扩展第2维:[2, 3]→[2, 3, 1]

a2=a.view(1,1,2,3)

print(a2.shape)#torch.Size([1, 1, 2, 3])

扩展多个维度:[2, 3]→[1, 1, 2, 3]

a2=a.view(1,1,2,1,1,3,1,1)

print(a2.shape)#torch.Size([1, 1, 2, 1, 1, 3, 1, 1])

扩展多个维度:[2, 3]→[1, 1, 2, 1, 1, 3, 1, 1]

a2=a.view(1,1,3,1,1,2,1,1)

print(a2.shape)#torch.Size([1, 1, 3, 1, 1, 2, 1, 1])

扩展多个维度:[2, 3]→[1, 1, 3, 1, 1, 2, 1, 1]

 

unsqueeze扩展一个维度

a=t.ones(2,3)

a2=a.unsqueeze(0)

print(a2.shape)# torch.Size([1, 2, 3])

扩展第0维:[2, 3]→[1, 2, 3]

a2=a.unsqueeze(1)

print(a2.shape)# torch.Size([2, 1, 3])

扩展第1维:[2, 3]→[2, 1, 3]

a2=a.unsqueeze(2)

print(a2.shape)# torch.Size([2, 3, 1])

扩展第2维:[2, 3]→[2, 3, 1]

a2=a.unsqueeze(3)

#IndexError: Dimension out of range (expected to be in range of [-3, 2], but got 3)

扩展第3维:Error

原文地址:http://www.cnblogs.com/zhangdezhang/p/16906777.html

1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长! 2. 分享目的仅供大家学习和交流,请务用于商业用途! 3. 如果你也有好源码或者教程,可以到用户中心发布,分享有积分奖励和额外收入! 4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解! 5. 如有链接无法下载、失效或广告,请联系管理员处理! 6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需! 7. 如遇到加密压缩包,默认解压密码为"gltf",如遇到无法解压的请联系管理员! 8. 因为资源和程序源码均为可复制品,所以不支持任何理由的退款兑现,请斟酌后支付下载 声明:如果标题没有注明"已测试"或者"测试可用"等字样的资源源码均未经过站长测试.特别注意没有标注的源码不保证任何可用性