import torch
a=torch.tensor([[3.0000, 3.0000],
[3.0000, 4.0000],
[3.6000, 3.0000],
[3.5000, 3.0000]])
个人以为这个unsqueeze方法有点废话,完全可以用reshape 实现,鸡肋!
a.reshape([a.shape[0],1,a.shape[1]])==a.unsqueeze(1)
a.reshape([1,a.shape[0],1,a.shape[1]])==a.unsqueeze(0)
a.reshape([a.shape[0],a.shape[1] ,1])==a.unsqueeze(2)
a.reshape([a.shape[0],1,a.shape[1]])==a.unsqueeze(1)
'''
0
m ,n------> 1,m,n
1
m ,n------> m,1,n
2
m ,n------> m,n,2
k
m ,.....,n------> m,...,k,k+1,n
'''
print('a=',a)
print('a.shape=',a.shape)
print('-'*30+'分割线'+'-'*30)
print('a.unsqueeze(0).shape=',a.unsqueeze(0).shape)
print('a.unsqueeze(0)=',a.unsqueeze(0))
print('-'*30+'分割线'+'-'*30)
print('a.unsqueeze(1).shape=',a.unsqueeze(1).shape)
print('a.unsqueeze(1)=',a.unsqueeze(1))
print('-'*30+'分割线'+'-'*30)
print('a.unsqueeze(2).shape=',a.unsqueeze(2).shape)
print('a.unsqueeze(2)=',a.unsqueeze(2))
在这里是一个二位矩阵 可以在[0,1,2]三个维度上
a= tensor([[3.0000, 3.0000],
[3.0000, 4.0000],
[3.6000, 3.0000],
[3.5000, 3.0000]])
a.shape= torch.Size([4, 2])
------------------------------分割线------------------------------
a.unsqueeze(0).shape= torch.Size([1, 4, 2])
a.unsqueeze(0)= tensor([[[3.0000, 3.0000],
[3.0000, 4.0000],
[3.6000, 3.0000],
[3.5000, 3.0000]]])
------------------------------分割线------------------------------
a.unsqueeze(1).shape= torch.Size([4, 1, 2])
a.unsqueeze(1)= tensor([[[3.0000, 3.0000]],
[[3.0000, 4.0000]],
[[3.6000, 3.0000]],
[[3.5000, 3.0000]]])
------------------------------分割线------------------------------
a.unsqueeze(2).shape= torch.Size([4, 2, 1])
a.unsqueeze(2)= tensor([[[3.0000],
[3.0000]],
[[3.0000],
[4.0000]],
[[3.6000],
[3.0000]],
[[3.5000],
[3.0000]]])
一个是顺序,一个逆序,形成一个闭环
'''
-3 -2 -1
0 1 2
'''
print('a.unsqueeze(-3)==a.unsqueeze(0)',a.unsqueeze(-3)==a.unsqueeze(0))
print('a.unsqueeze(-2)==a.unsqueeze(1)',a.unsqueeze(-2)==a.unsqueeze(1))
print('a.unsqueeze(-1)==a.unsqueeze(2)',a.unsqueeze(-1)==a.unsqueeze(2))
a.unsqueeze(-3)==a.unsqueeze(0) tensor([[[True, True],
[True, True],
[True, True],
[True, True]]])
a.unsqueeze(-2)==a.unsqueeze(1) tensor([[[True, True]],
[[True, True]],
[[True, True]],
[[True, True]]])
a.unsqueeze(-1)==a.unsqueeze(2) tensor([[[True],
[True]],
[[True],
[True]],
[[True],
[True]],
[[True],
[True]]])
标签:3.0000,unsqueeze,tensor,torch,用法,shape,print,True
From: https://blog.51cto.com/u_15202985/6005763