【问题标题】:How to convert (1,64,224,224) --> (1,64) using adaptive average pooling(Pytorch)?如何使用自适应平均池(Pytorch)转换(1,64,224,224)->(1,64)?
【发布时间】:2021-06-29 19:26:54
【问题描述】:

Pytorch 有什么方法可以减少模型中张量的维数吗?

【问题讨论】:

  • 请把相关答案标记为正确

标签: pytorch average dimensionality-reduction pooling


【解决方案1】:

自适应平均池化或 Pytorch 中的任何典型池化都不会减少张量的维度。 你可以在这里找到 Pytorch 提供的所有类型的池: https://pytorch.org/docs/master/nn.html#pooling-layers

我建议使用此模板代码来尝试不同的池及其对维度的影响:

m = nn.AdaptiveAvgPool2d((5,7))
input = torch.randn(1, 64, 8, 9)
output = m(input)
print(output.size())

为了减少 Pytorch 模型中的维度,您可以指定一个对张量执行挤压()的块,甚至使用 example_tensor.view(-1, x, y) 将张量展平。

萨塔克·耆那教

【讨论】:

    【解决方案2】:

    这段代码应该可以压缩 (1,64,224,224) --> (1,64)

    import torch
    import torch.nn as nn
    m = nn.AdaptiveAvgPool2d((1,1))
    input = torch.randn(1, 64, 224, 224)
    output = m(input).view(1,-1)
    print(output.size())  #torch.Size([1, 64])
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-04-03
      • 2019-05-19
      • 2023-03-30
      • 2019-11-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-27
      相关资源
      最近更新 更多