维度的变换

view 和 reshape 操作

二者的功能和用法是相同的,不过要保证 view 的size整体不变,即numel()一致

view操作会把维度信息给丢失掉,要记牢数据的存储顺序

a = torch.rand(4,1,28,28)
b = a.view(4,28*28)     #丢失两个维度
print(b)
print(b.shape)
c = a.view(4*28,28)     #丢失两个维度
d = a.view(4*1,28,28)   #丢失一个维度

e = a.reshape(4*1,28,28)        #和view一样
print(c.shape)
print(d.shape)
print(e.shape)


 Squeeze 与 Unsqueeze

挤压与展开


 unsqueeze

新插入一个维度,这个维度不会改变数据本身,不会增加也不会减少数据,相当于增加了一个组别

a = torch.rand(4,1,28,28)
b = a.unsqueeze(0)      #在第0个维度前面添加1维度
c = a.unsqueeze(4)      #在第4个维度前面添加1维度
d = a.unsqueeze(-1)     #在最后一个维度后面添加1维度
e = a.unsqueeze(-4)
f  = a.unsqueeze(-5)
print(b.shape),print(c.shape),print(d.shape)
print(e.shape),print(f.shape)


 它还可以改变tensor的形状

a = torch.tensor([1,2])
b = a.unsqueeze(0)      #在外面增加一个维度
c = a.unsqueeze(1)      #在里面增加一个维度

print(a)
print(a.shape)
print(b)
print(c)


它还可以嵌套多次~扩张维度

#a = torch.rand(4,1,28,30)  #可以使用unsqueeze将一个D的展开为3D,然后再扩张成为a

a = torch.rand(28)
b = a.unsqueeze(0)
c = a.unsqueeze(0).unsqueeze(0)
d = a.unsqueeze(0).unsqueeze(0).unsqueeze(3)
print(b.shape),print(c.shape),print(d.shape)

 


squeeze

挤压,顾名思义这里就是维度删减

可以接收正的和负的索引,如果当前索引的数字是1,就把这个维度去掉,否则不变,

如果不给索引,默认把所有符合条件的去掉

a = torch.rand(1,32,1,1)
b = a.squeeze()      #什么都不写,默认把所有size为1的删去
c = a.squeeze(0)    #若索引位置是1,就把这一维去掉,否则不变
d = a.squeeze(1)    #不变
e = a.squeeze(-1)
print(b.shape),print(c.shape)
print(d.shape),print(e.shape)


expand 和 repeat

二者都可以扩展一个张量的shape,是等效的,不过repeat有弊端,推荐 expand


expand

不会主动地复制数据,速度快,节约内存

它的扩张两个条件

        1、只有当前维度size为1 时才任意扩张,否则不能。

        2、扩张前后的维度不变

#   a = torch.rand(4,32,14,14)
b = torch.rand(1,32,1,1)    #接下来由 b 扩张为 a
ba = b.expand(4,32,14,14)
print(ba.shape)
c = b.expand(-1,-1,4,14)    #输入-1,则返回原来对应的数据
print(c.shape)


repeat 

repeat( ) 它给的参数不再是扩张后的数据,而是每个维度要重复的次数。会更改原来的数据

两个条件:

        1、维度前后不变

        2、给的参数是每个维度重复的次数,要自己计算

a = torch.rand(1,12,1,1)
b = a.repeat(4,12,8,13)         #每个维度要重复的次数
c = a.repeat(4,1,12,12)
print(b.shape)
print(c.shape)


.t                transpose                permute

三种维度转换操作



.t                转置操作

这种方法只适用于二维,即矩阵

a = torch.randn(2,4)
b = a.t()
print(a),print(b)


transpose                 维度交换

transpose、permute等维度变换操作后,tensor在内存中不再是连续存储的,而view操作要求tensor的内存连续存储,所以需要contiguous来返回一个contiguous copy

view会使维度顺序关系模糊,需要人为跟踪

a = torch.rand(4,3,32,32)
b = a.transpose(1,3)        #表示要交换的维度
print(b.shape)

a1 = a.transpose(1,3).contiguous().view(4,3*32*32).view(4,3,32,32)  #第一维交换了没换回来,即使shape一样,实际不一样
a2 = a.transpose(1,3).contiguous().view(4,3*32*32).view(4,32,32,3).transpose(1,3)#view后要记住顺序,得换回来才是原样

print(a1.shape),print(a2.shape)
print(torch.all(torch.eq(a,a1)))       #判断内容是否不变
print(torch.all(torch.eq(a,a2)))        #eq函数比较内容是不是一致,all返回TRUE or FALSE


permute        交换维度

它可以完成任意维度的交换,在交换的过程中相当于调用任意多次的transpose

permute()使用

        用原来shape的索引放置的位置确定谁与谁交换

a = torch.rand(4,3,28,28)
b = a.transpose(1,3).transpose(0,2)
p = a.permute(2,3,0,1)  #一步到位
print(b.shape)
print(p.shape)

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

onlywishes

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值