深度学习中,本人将训练好的模型,通过保存完整模型和只保存模型参数两种方式对模型进行了保存,在分别加载两种模型,并输入同一测试集后得到的准确度却相差较大,该怎么解决?
#模型保存
torch.save(net, 'net.pth')
torch.save(net.state_dict(), 'network_params.pth')
#加载
net1 = torch.load('net.pth')
output1 = evaluate_accuracy(test_data, net1)
net2 = Net()
net2.load_state_dict(torch.load('network_params.pth'),False)
output2 = evaluate_accuracy(test_data, net2)
#准确度评估
def evaluate_accuracy(test_data, net):
net.eval()
test_acc_sum= 0.0
n = 0
with torch.no_grad():
for x,y in test_data:
x = torch.unsqueeze(x, dim=1)
x = torch.unsqueeze(x, dim=1)
test_acc_sum += (net(x).argmax(dim=1) == y.argmax(dim=1)).sum().item()
n += y.shape[0]
return test_acc_sum / n