【问题标题】:AttributeError: 'torch.return_types.max' object has no attribute 'dim' - Maxpooling ChannelAttributeError: 'torch.return_types.max' 对象没有属性 'dim' - Maxpooling Channel
【发布时间】:2020-07-05 21:28:51
【问题描述】:

我正在尝试对通道维度进行最大池化:

class ChannelPool(nn.Module):
    def forward(self, input):
        return torch.max(input, dim=1)

但我得到了错误

AttributeError: 'torch.return_types.max' object has no attribute 'dim'

【问题讨论】:

    标签: python computer-vision pytorch


    【解决方案1】:

    使用dim 调用的torch.max 函数返回一个元组,因此:

    class ChannelPool(nn.Module):
        def forward(self, input):
            input_max, max_indices = torch.max(input, dim=1)
            return input_max
    

    来自documentation of torch.max

    返回一个命名元组 (values, indices),其中 values 是给定维度 dim 中输入张量的每一行的最大值。 index 是找到的每个最大值的索引位置(argmax)。

    【讨论】:

      【解决方案2】:

      我最近遇到了同样的错误。 torch.max()有两种形式。

      • 如果你只给出一个输入张量(没有其他参数,如dim ...),max() 函数将返回一个张量

      • 如果您指定其他参数(例如dim=0),max() 函数将返回一个命名元组:(值,索引)。我猜values 是你想要的。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-03-12
        • 1970-01-01
        • 2012-12-01
        • 2021-03-06
        • 2021-04-19
        • 2021-11-22
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多