【问题标题】:Loading PyTorch model and receiving error "AttributeError: Can't get attribute 'Model' on <module '__main__'>"加载 PyTorch 模型并收到错误“AttributeError: Can't get attribute 'Model' on <module '__main__'>”
【发布时间】:2022-04-06 20:30:58
【问题描述】:

我正在使用 torch.load() 加载 Pytorch 模型并收到错误“AttributeError: Can't get attribute 'Model' on ma​​in'>”。有谁知道是什么原因造成的?

【问题讨论】:

  • 我有一个发送给我的 .pt 文件,我正在使用 torch.load(PATH),所以这是模型的问题吗?
  • 你也有一个包含正确模型架构类的python文件吗?

标签: machine-learning pytorch


【解决方案1】:

已保存的权重必须有一个类。您应该与加载的权重保持相同的类和文件架构。

因为torch.load() 隐式使用了pickle 模块。

【讨论】:

    【解决方案2】:
    1. 即使我也面临同样的错误。这想说的是通过调用类创建模型的实例,然后执行torch.load()
    2. 如果通过 PyTorch 访问 Saving and Loading Models 上的博客之一,并且在 load 部分中,您可以清楚地看到 # Model class must be defined somewhere 这一行。
    3. 请执行以下操作:
    model = ModelClass()
    model = torch.load(PATH, , map_location=torch.device('cpu')) #<--- if current device is 'CPU'
    model.eval() #<---- To prevent it from going to retraining mode.
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-08-24
      • 2021-10-24
      • 2022-08-13
      • 2022-10-25
      • 1970-01-01
      • 1970-01-01
      • 2019-09-23
      • 2021-12-30
      相关资源
      最近更新 更多