【问题标题】:Pytorch inceptionV3 transfer learning gives error - max() received an invalid combination of argumentsPytorch inceptionV3 迁移学习给出错误 - max() 收到了无效的参数组合
【发布时间】:2018-12-05 08:23:07
【问题描述】:

我正在使用的pytorch中的迁移学习inception_v3程序在这里:https://drive.google.com/file/d/1zn4z7nOp_wJne0En6zq4WJfwHVVftERT/view?usp=sharing

运行程序时出现以下错误:

Epoch 0/24   
    ---------------------------------------------------------------------------
    TypeError                                 Traceback (most recent call last)
    <ipython-input-20-cc88ea5f8bd3> in <module>()
          1 model_ft = train_model(model_ft, criterion, optimizer_ft, exp_lr_scheduler,
    ----> 2                        num_epochs=25)

    <ipython-input-17-812cf3c4576a> in train_model(model, criterion, optimizer, scheduler, num_epochs)
         33                     outputs = model(inputs)
         34                     print(outputs)
    ---> 35                     _, preds = torch.max(outputs, 1)
         36                     loss = criterion(outputs, labels)
         37 

    TypeError: max() received an invalid combination of arguments - got (tuple, int), but expected one of:
     * (Tensor input)
     * (Tensor input, Tensor other, Tensor out)
     * (Tensor input, int dim, bool keepdim, tuple of Tensors out)

如何解决这个问题? 谢谢

【问题讨论】:

  • 你的模型输出的是 Torch Tensor 吗?
  • 现在已经解决了。谢谢

标签: python deep-learning pytorch convolutional-neural-network


【解决方案1】:

在这种情况下,我已将代码更改如下,它对我有用, 教程https://pytorch.org/tutorials/beginner/transfer_learning_tutorial.html

model_ft = models.inception_v3(pretrained=True)
model_ft.aux_logits=False

【讨论】:

  • 为什么会这样?这会损害模型性能吗?您提供的链接没有讨论这个...
【解决方案2】:
model_ft.aux_logits=False

model_ft.fc = nn.Linear(2048, 2)

【讨论】:

  • 如果您解释了您提供的代码如何回答问题,这将是一个更好的答案。
  • 这是我对这个问题的经验。我真的对InceptionV3的内部架构一无所知。在实验中,用户复制 ResNet 的迁移学习代码并将 ResNet 名称更改为 Inception V3。然后他们运行代码并出现错误。因此,他们可以根据我的经验解决问题。
  • “这是我的经验”并没有真正解释您的代码是如何工作的。为什么将aux_logits 属性设置为False 然后调用Linear 方法是个好主意。这些事情中的每一个都有什么作用? 这段代码是如何工作的?
【解决方案3】:

该行应如下所示:

_, preds = torch.max(outputs.data, 1)

【讨论】:

    【解决方案4】:

    我从这里发现了问题:pytorch inceptionv3 int line 125.The error is when in train mod and aux_logits opening,它返回(x, aux)。 我通过

    解决了
    1. 使用output, aux = model(input_var)

    2. 如果相位=='train': 输出,辅助=模型(输入) 别的: 输出=模型(输入)

    【讨论】:

      猜你喜欢
      • 2019-07-26
      • 2020-08-27
      • 2019-06-12
      • 1970-01-01
      • 2016-05-18
      • 2018-08-04
      • 2020-11-07
      • 1970-01-01
      • 2021-11-30
      相关资源
      最近更新 更多