#添加到学习笔记2末尾,直接运行。代码意义可以看注释。
print('——————————维度变换——————————')
va=torch.rand(4,1,28,28)
print('tensor shape:',va.shape)
vb=va.view(4,1*28*28)#把va后面三个维度合并,变成一个二维tensor,元素个数保持不变
print('tensor shape:',vb.shape)
vc=va.view(4*1*28,28)#把va前面三个维度合并,变成一个二维tensor,元素个数保持不变
print('tensor shape:',vc.shape)
vd=va.unsqueeze(0)#在第一维前面增加一个维度
print('tensor shape:',vd.shape)
ve=va.unsqueeze(-1)#在最后一维后面增加一个维度
print('tensor shape:',ve.shape)
vf=va.unsqueeze(4)#在最后一维后面增加一个维度,建议都用正数,在正数序号之前插入维度
print('tensor shape:',vf.shape)
vg=torch.rand(32)#把vg扩展成vh的维度
vh=torch.rand(4,32,14,14)
vg=vg.unsqueeze(1).unsqueeze(2).unsqueeze(0)#[32]->[32,1]->[32,1,1]->[1,32,1,1]
vi=torch.rand(1,32,1