当前位置:   article > 正文

torch.squeeze() dim=1 dim=-1 dim=2_torch .squeeze(dim=1)

torch .squeeze(dim=1)

对数据的维度进行压缩

使用方式:torch.squeeze(input, dim=None, out=None)

将输入张量形状中的1 去除并返回。 如果输入是形如(A×1×B×1×C×1×D),那么输出形状就为: (A×B×C×D)

  1. import torch
  2. x = torch.rand(2, 1, 1, 3, 1, 4)
  3. print('=======x=========')
  4. print(x.shape)
  5. out_1 = torch.squeeze(x)
  6. print('=======out_1=========')
  7. print(out_1.shape)
  8. # =======x=========
  9. # torch.Size([2, 1, 1, 3, 1, 4])
  10. # =======out_1=========
  11. # torch.Size([2, 3, 4])

当给定dim时,那么挤压操作只在给定维度上。例如,输入形状为: (A×1×B), squeeze(input, 0) 将会保持张量不变,只有用 squeeze(input, 1),形状会变成 (A×B)。

注意:

如果dim指定的维度的值为1

第一种情况

  1. import torch
  2. x = torch.rand(2,1,1,3,1,4)
  3. print('=======x=========')
  4. print(x.shape)
  5. out_1 = torch.squeeze(x, dim=1)
  6. print('=======out_1=========')
  7. print(out_1.shape)
  8. # =======x=========
  9. # torch.Size([2, 1, 1, 3, 1, 4])
  10. # =======out_1=========
  11. # torch.Size([2, 1, 3, 1, 4])
  1. import torch
  2. x = torch.rand(2,1,3,1,4)
  3. print('=======x=========')
  4. print(x.shape)
  5. out_1 = torch.squeeze(x, dim=1)
  6. print('=======out_1=========')
  7. print(out_1.shape)
  8. # =======x=========
  9. # torch.Size([2, 1, 3, 1, 4])
  10. # =======out_1=========
  11. # torch.Size([2, 3, 1, 4])

第二种情况

  1. x = torch.rand(1,2,1,1,3,1,4)
  2. print('=======x=========')
  3. print(x.shape)
  4. out_2 = torch.squeeze(x, dim=1)
  5. print('=======out_2=========')
  6. print(out_2.shape)
  7. # =======x=========
  8. # torch.Size([1, 2, 1, 1, 3, 1, 4])
  9. # =======out_2=========
  10. # torch.Size([1, 2, 1, 1, 3, 1, 4])

第三种情况

  1. x = torch.rand(1,1,2,1,1,3,1,4)
  2. print('=======x=========')
  3. print(x.shape)
  4. out_3 = torch.squeeze(x, dim=1)
  5. print('=======out_3=========')
  6. print(out_3.shape)
  7. # =======x=========
  8. # # torch.Size([1, 1, 2, 1, 1, 3, 1, 4])
  9. # # =======out_3=========
  10. # # torch.Size([1, 2, 1, 1, 3, 1, 4])

如果dim指定的维度的值为-1

第一种情况 如果dim指定的维度的值为-1

  1. import torch
  2. x = torch.rand(2,1,1,3,1,4)
  3. print('=======x=========')
  4. print(x.shape)
  5. out_1 = torch.squeeze(x, dim=-1)
  6. print('=======out_1=========')
  7. print(out_1.shape)
  8. # =======x=========
  9. # torch.Size([2, 1, 1, 3, 1, 4])
  10. # =======out_1=========
  11. # torch.Size([2, 1, 1, 3, 1, 4])

第二种情况 如果dim指定的维度的值为-1

  1. x = torch.rand(2,1,1,3,1,4,1)
  2. print('=======x=========')
  3. print(x.shape)
  4. out_2 = torch.squeeze(x, dim=-1)
  5. print('=======out_2=========')
  6. print(out_2.shape)
  7. # =======x=========
  8. # torch.Size([2, 1, 1, 3, 1, 4, 1])
  9. # =======out_2=========
  10. # torch.Size([2, 1, 1, 3, 1, 4])

第三种情况 如果dim指定的维度的值为-1

  1. x = torch.rand(2,1,1,3,1,4,1,1)
  2. print('=======x=========')
  3. print(x.shape)
  4. out_3 = torch.squeeze(x, dim=-1)
  5. print('=======out_3=========')
  6. print(out_3.shape)
  7. # =======x=========
  8. # torch.Size([2, 1, 1, 3, 1, 4, 1, 1])
  9. # =======out_3=========
  10. # torch.Size([2, 1, 1, 3, 1, 4, 1])

如果dim指定的维度的值为2

  1. import torch
  2. x = torch.rand(2,1,3,1,4)
  3. print('=======x=========')
  4. print(x.shape)
  5. out_1 = torch.squeeze(x, dim=2)
  6. print('=======out_1=========')
  7. print(out_1.shape)
  8. # =======x=========
  9. # torch.Size([2, 1, 3, 1, 4])
  10. # =======out_1=========
  11. # torch.Size([2, 1, 3, 1, 4])
  12. x = torch.rand(2,1,1,3,1,4)
  13. print('=======x=========')
  14. print(x.shape)
  15. out_2 = torch.squeeze(x, dim=2)
  16. print('=======out_2=========')
  17. print(out_2.shape)
  18. # =======x=========
  19. # torch.Size([2, 1, 1, 3, 1, 4])
  20. # =======out_2=========
  21. # torch.Size([2, 1, 3, 1, 4])
  22. x = torch.rand(1,2,1,1,3,1,1,4)
  23. print('=======x=========')
  24. print(x.shape)
  25. out_3 = torch.squeeze(x, dim=2)
  26. print('=======out_3=========')
  27. print(out_3.shape)
  28. # =======x=========
  29. # torch.Size([1, 2, 1, 1, 3, 1, 1, 4])
  30. # =======out_3=========
  31. # torch.Size([1, 2, 1, 3, 1, 1, 4])

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/739587
推荐阅读
相关标签
  

闽ICP备14008679号