Pytorch——tensor维度变换

1、常用的api

(1)View/reshape 可以将一个shape转变成任意一个shape

(2)Squeeze/unsqueeze 挤压与增加维度

(3)Transpose/t/permute (矩阵的传置) 单次的交换操作和多次的交换操作

(4)Expand/repeat 维度的扩展 我们可以把维度小的变成高维度的

2、view 和reshape

这两个基本是一摸一样的,区别在于pytorch0.3中默认的是view函数;

a=torch.rand(4,1,28,28) #使用rand()函数随机均匀的初始化生成一个四维的tensor
#28,28 表示图片的长和宽,1表示一个通道,表示是灰度图片,4表示一共有四张图片
print(a.shape) #输出结果 torch.Size([4,1,28,28])



print(a.view(4,28*28)) #把所有信息合并在一起也就是[4,784],像图片这种二维的信息必须要打平

print(a.view(4,28*28).shape) # 输出结果 torch.Size([4,784)

print(a.view(4*28,28).shape) #输出结果 torch.Size([112,28)
#把原来数据的前三个通道合并在一起4*1*28

print(a.view(4*1,28,28).shape) # 输出结果 torch.Size([4,28,28)


b=a.view(4,784) # 数据的存储/维度顺序非常重要

print(b.view(4,28,28,1)) #输出的数据就会出问题,与原来的数据不一致

3、Squeeze/unsqueeze 挤压(减少维度)与增加维度

a=torch.rand(4,1,28,28) #使用rand()函数随机均匀的初始化生成一个四维的tensor
#28,28 表示图片的长和宽,1表示一个通道,表示是灰度图片,4表示一共有四张图片
print(a.shape) #输出结果 torch.Size([4,1,28,28])


print(a.unsqueeze(0).shape)#表示再原来的维度前面加入一个维度,没有增加数据,再哪一个索引前面插入
#输出结果 torch.Size([1,4,1,28,28])


print(a.unsqueeze(-1).shape) #表示在-1维度后面插入
#输出结果 torch.Size([4,1,28,28,1])

print(a.unsqueeze(4).shape) #表示在4维度前面插入
#输出结果 torch.Size([1,4,1,28,28])

print(a.unsqueeze(-4).shape) #表示在-4维度后面插入
#输出结果 torch.Size([4,1,1,28,28])

print(a.unsqueeze(-5).shape) #输出结果 torch.Size([1,4,1,28,28])

print(a.unsqueeze(5).shape) #输出结果报错

a=torch.rand(4,1,28,28) #使用rand()函数随机均匀的初始化生成一个四维的tensor
#28,28 表示图片的长和宽,1表示一个通道,表示是灰度图片,4表示一共有四张图片
print(a.shape) #输出结果 torch.Size([4,1,28,28])


b=torch.rand(32)
f=torch.rand(4,32,14,14)
b=b.unsqueeze(1).unsqueeze(2).unsqueeze(0)
print(b.shape) #输出结果torch.Size([1,32,1,1])

print(b.squeeze().shape)#表示把可以删减的维度全部删剪掉,是1就可以被挤压掉
 #输出结果torch.Size([32])


print(b.squeeze(0).shape) #表示把第0个维度的挤压掉
#输出结果torch.Size([32,1,1])

print(b.squeeze(-1).shape) 
#输出结果torch.Size([1,32,1])

print(b.squeeze(1).shape) #表示第一维的时数据32 ,不能被挤压,所以返回不变
#输出结果torch.Size([1,32,1,1])


print(b.squeeze(-4).shape) #输出结果torch.Size([32,1,1])

4、Transpose/t/permute

b.t()  矩阵的转置

print(a.shape) #输出结果[4,3,32,32]

a1=a.transpose(1,3).view(4,3*32*32).view(4,3,32,32) #数据的维度交换

a1=a.transpose(1,3).contiguous().view(4,3*32*32).view(4,3,32,32) #这种方式是错的

a2=a.transpose(1,3).contiguous().view(4,3*32*32).view(4,3,32,32).transpose(1,3) #正确表示方法

print(a1.shape) #输出结果[4,3,32,32]
print(a2.shape) #输出结果[4,3,32,32]

#验证
torch.all(troch.eq(a,a1))  #结果0 错误,数据不一致

torch.all(troch.eq(a,a2))  #结果1 正确 数据一致
a=torch.rand(4,1,28,28) #使用rand()函数随机均匀的初始化生成一个四维的tensor
print(a.transpose(1,3).shape) #结果[4,28,28,1]

b=torch.rand(4,1,28,32) 
print(b.transpose(1,3).shape) #结果[4,32,28,1]

print(b.transpose(1,3).transpose(1,2).shape) #结果[4,28,32,1]

#更简单的方法
print(b.permute(0,2,3,1)) #结果[4,28,32,1]

5、Expand/repeat

Expand:增加了维度,并没有增加数据,不会主动的复制数据,推荐使用

repeat:是实实在在增加了数据,

a=torch.rand(4,32,14,14)
print(b.shape) #输出结果torch.Size([1,32,1,1])

print(b.expend(4,32,14,14).shape) #直接扩展,原来的dim和要扩展之后的dim必须一致
#输出结果torch.Size([4,32,14,14])

print(b.expend(-1,32,-1,-1).shape) #-1表示保持原来的维度不变
#输出结果torch.Size([1,32,1,1])

print(b.expend(-1,32,-1,-4).shape) #这个是没有意义的  bug
#输出结果torch.Size([1,32,1,-4])
print(b.shape) #输出结果torch.Size([1,32,1,1])

print(b.repeat(4,32,1,1).shape) #1*4  32*32 1*1 1*1
#输出结果torch.Size([4,1024,1,1])

print(b.repeat(4,1,1,1).shape) #输出结果torch.Size([4,32,1,1])

print(b.repeat(4,1,32,32).shape) #输出结果torch.Size([4,32,32,32])



猜你喜欢

转载自blog.csdn.net/qq_42012782/article/details/124115340