首页 > 其他分享 >pytorch学习笔记(1)

pytorch学习笔记(1)

时间:2022-11-19 19:12:51浏览次数:45  
标签:torch shape 笔记 学习 print pytorch a2 维度 Size

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

标签:torch,shape,笔记,学习,print,pytorch,a2,维度,Size
From: https://www.cnblogs.com/zhangdezhang/p/16906777.html

相关文章

  • 【《硬件架构的艺术》读书笔记】02 时钟和复位zui'xiao'zhi1
    2.6.1用同步复位进行设计    上面两个电路功能一样,但是下面的电路如果load信号为X,触发器便会停在不定态。可以使用编译指令告诉指定的信号为复位信号,综合工具就......
  • PHY8. 学习规范场论
    发现这种写学习日记的方式在一定程度上有助于提高学习效率……所以来开新坑了。规范场论本是量子场论的一部分,但在课程安排中它将不涉及那些基础的理论知识,而是逐渐步入场......
  • 工业场景全流程!机器学习开发并部署服务到云端 ⛵
    ......
  • 20201322学习笔记12
    第十四章MySQL数据库系统14.1Mysql简介MySQL是一个关系数据库系统。在关系数据库中,数据存储在表中。每个表由多个行和列组成。表中的数据相互关联。表也可能与其他表有......
  • 计算机网络学习
    目录1.计算机网络概述1.1计算机网络基本概念1.1.1什么是计算机网络?1.1.1.1计算机网络=通信技术+计算机技术1.1.1.2计算机网络的定义1.1.1.3交换机1.1.1.4网络Inter......
  • orcale笔记04-DQL语言
    单表查询:select字段1,字段2,... from 表名 whereconfidentconfident  精确查找=,范围查找>,<,>=,<=,......
  • orcale笔记03-DML语句
    insertinto:插入数据全表插入:insertinto表名values(值1,值2...);部分列插入:insertinto表名(列1,列2...)values(值1,值2...)从其他表中复制数据:insertin......
  • 深度学习与通信交叉领域的python包:deepcom
    什么是deepcom在进行深度学习与通信领域的交叉研究时,有一些反复使用的算法与训练流程。但是现有的学习框架主要集中在网络的训练部分,对于通信领域的参数压缩与高效传输并......
  • orcale笔记02 DDL语言
    create创建对象alter 修改对象drop删除对象truncate清空对象创建表:createtable表名(字段1数据类型约束条件,字段2数据类型约束条......
  • Python学习笔记(三)
    运算符和表达式算术运算python在这里直接支持了幂运算,c的话需要额外的头文件导入此外,python也是支持取模%和取整运算的。The / (division)and // (floordivisi......