首页 > 其他 > 详细

squeeze

时间:2019-09-29 16:03:44      阅读:56      评论:0      收藏:0      [点我收藏+]
torch.squeeze() 对数据的维度进行压缩,去掉维数为1的的维度,默认是将a中所有为1的维度删掉。
也可以通过dim指定位置,删掉指定位置的维数为1的维度。

torch.unsqueeze()对数据维度进行
import torch

x = torch.zeros(3, 2, 4, 1, 2, 1)  # dimension of 3*2*4*1*2
print(x.size())  # torch.Size([3, 2, 4, 1, 2, 1])
print(x.shape) # torch.Size([3, 2, 4, 1, 2, 1])

y = torch.squeeze(x)  # Returns a tensor with all the dimensions of input of size 1 removed.
print(y.size())  # torch.Size([3, 2, 4, 2])
print(y.shape)

z = torch.unsqueeze(y, dim=0)  # Add a dimension of 1 in the 0th position
print(z.size())  # torch.Size([1, 3, 2, 4, 2])
print(z.shape)

z = torch.unsqueeze(y, dim=1)  # Add a dimension of 1 in the 1st position
print(z.size())  # torch.Size([3, 1, 2, 4, 2])
print(z.shape)

z = torch.unsqueeze(y, dim=2)  # Add a dimension of 1 in the 2nd position
print(z.size())  # torch.Size([3, 2, 1, 4, 2])
print(z.shape)

y = torch.squeeze(x, dim=0)  # remove the 0th position of 1 (no 1)
print(dim=0, y.size())  # torch.Size([3, 2, 4, 1, 2, 1])
print(dim=0, y.shape)

y = torch.squeeze(x, dim=1)  # remove the 1st position of 1 (no 1)
print(dim=1, y.size())  # torch.Size([3, 2, 4, 1, 2, 1])
print(dim=1, y.shape)

y = torch.squeeze(x, dim=2)  # remove the 2nd position of 1 (no 1)
print(dim=2, y.size())  # torch.Size([3, 2, 4, 1, 2])
print(dim=2, y.shape)

y = torch.squeeze(x, dim=3)  # remove the 3rd position of 1 (yes)
print(dim=3, y.size())  # torch.Size([3, 2, 4, 2, 1])
print(dim=3, y.shape)

y = torch.squeeze(x, dim=4)  # remove the 4th position of 1 (no 1)
print(dim=4, y.size())  # torch.Size([3, 2, 4, 1, 2, 1])
print(dim=4, y.shape)

y = torch.squeeze(x, dim=5)  # remove the 5th position of 1 (yes)
print(dim=5, y.size())  # torch.Size([3, 2, 4, 1, 2])
print(dim=5, y.shape)

y = torch.squeeze(x, dim=6)  # RuntimeError: Dimension out of range (expected to be in range of [-6, 5], but got 6)
print(dim=6, y.size())
print(dim=6, y.shape)

 

扩充。需要通过dim指定位置,给指定位置加上维数为1的维度。

squeeze

原文:https://www.cnblogs.com/hapyygril/p/11607974.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!