且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

加载预训练模型 pytorch - dict 对象没有属性 eval

更新时间:2023-12-01 21:40:34

首先,您已经说明了您的模型.而torch.load() 会给你一本字典.那本字典没有 eval 函数.所以你应该将权重上传到你的模型中.

First, you have stated your model. And torch.load() gives you a dictionary. That dictionary has not an eval function. So you should upload the weights to your model.

import torch
from modelfolder import yourmodel

model = yourmodel()
checkpoint = torch.load('model_best.pth.tar')
try:
    checkpoint.eval()
except AttributeError as error:
    print error
### 'dict' object has no attribute 'eval'

model.load_state_dict(checkpoint['state_dict'])
### now you can evaluate it
model.eval()