当前位置:   article > 正文

全局平均池化的示例

全局平均池化的示例

1.对一个3通道,5*5的矩阵,进行全局平均池化

每个矩阵的大小都是 5x5,假设这些矩阵代表一幅图像的三个不同通道。为简单起见,我们将这三个矩阵分别称为 A、B 和 C。合成图像将是一个三通道图像,每个通道由其中一个矩阵表示。

  1. A = [[a11, a12, a13, a14, a15],
  2. [a21, a22, a23, a24, a25],
  3. [a31, a32, a33, a34, a35],
  4. [a41, a42, a43, a44, a45],
  5. [a51, a52, a53, a54, a55]]
  6. B = [[b11, b12, b13, b14, b15],
  7. [b21, b22, b23, b24, b25],
  8. [b31, b32, b33, b34, b35],
  9. [b41, b42, b43, b44, b45],
  10. [b51, b52, b53, b54, b55]]
  11. C = [[c11, c12, c13, c14, c15],
  12. [c21, c22, c23, c24, c25],
  13. [c31, c32, c33, c34, c35],
  14. [c41, c42, c43, c44, c45],
  15. [c51, c52, c53, c54, c55]]

现在,全局平均池操作将独立应用于每个通道。对于每个通道,它会计算该通道中所有元素的平均值。计算结果是一个向量,每个通道只有一个值。

  1. avg_A = (a11 + a12 + ... + a55) / 25
  2. avg_B = (b11 + b12 + ... + b55) / 25
  3. avg_C = (c11 + c12 + ... + c55) / 25

这样,经过全局平均汇集后的合成图像将是一个 3 通道图像,每个通道由其原始矩阵的平均值表示: 

Composite Image = [[avg_A, avg_B, avg_C]]

2.torch示例

  1. import torch
  2. import torch.nn as nn
  3. # Generate a random 3-channel matrix with integer values for a batch of size 2
  4. torch.manual_seed(42) # Setting seed for reproducibility
  5. batch_size = 1
  6. image_matrix = torch.randint(0, 10, (batch_size, 3, 5, 5), dtype=torch.float32) # Batch size 2, 3 channels, 5x5 matrix
  7. # Display the original matrix
  8. print("Original Matrix:")
  9. print(image_matrix)
  10. # Apply global average pooling using nn.AdaptiveAvgPool2d
  11. adaptive_avg_pool = nn.AdaptiveAvgPool2d(1)
  12. global_avg_pooled = adaptive_avg_pool(image_matrix)
  13. # Display the result after global average pooling
  14. print("\nResult after Global Average Pooling:")
  15. print(global_avg_pooled)
  1. Original Matrix:
  2. tensor([[[[2., 7., 6., 4., 6.],
  3. [5., 0., 4., 0., 3.],
  4. [8., 4., 0., 4., 1.],
  5. [2., 5., 5., 7., 6.],
  6. [9., 6., 3., 1., 9.]],
  7. [[3., 1., 9., 7., 9.],
  8. [2., 0., 5., 9., 3.],
  9. [4., 9., 6., 2., 0.],
  10. [6., 2., 7., 9., 7.],
  11. [3., 3., 4., 3., 7.]],
  12. [[0., 9., 0., 9., 6.],
  13. [9., 5., 4., 8., 8.],
  14. [6., 0., 0., 0., 0.],
  15. [1., 3., 0., 1., 1.],
  16. [7., 9., 4., 3., 8.]]]])
  17. Result after Global Average Pooling:
  18. tensor([[[[4.2800]],
  19. [[4.8000]],
  20. [[4.0400]]]])

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

闽ICP备14008679号