加载检查点时如何修复AttributeError?

时间:2019-06-20 13:18:50

标签: python artificial-intelligence attributeerror

我正在使用Udacity(Python编程的人工智能)来开发课程2的项目。

我已经训练了一个模型并将其保存在checkpoint.pth中,我想加载checkpoint.pth以便重建模型。

我已经编写了代码来保存checkpoint.pth并加载检查点。

model.class_to_idx = image_datasets['train_dir'].class_to_idx

model.cpu()

checkpoint = {'input_size': 25088,
              'output_size': 102,
              'hidden_layers': 4096,
              'epochs': epochs,
              'optimizer': optimizer.state_dict(),
              'state_dict': model.state_dict(),
              'class_to_index' : model.class_to_idx
             }


torch.save(checkpoint, 'checkpoint.pth')

def load_checkpoint(filepath):
    checkpoint = torch.load(filepath)

    model = checkpoint.Network(checkpoint['input_size'],
                               checkpoint['output_size'],
                               checkpoint['hidden_layers'],
                               checkpoint['epochs'],
                               checkpoint['optimizer'],
                               checkpoint['class_to_index']
                              )
    model.load_state_dict(checkpoint['state_dict'])

    return model

model = load_checkpoint('checkpoint.pth')

在加载checkpoint.pth时,出现错误:

AttributeError: 'dict' object has no attribute 'Network'

我要成功加载检查点。

谢谢

1 个答案:

答案 0 :(得分:0)

更新:通过完整的代码可见,我认为问题出在实现中。 torch.load将已反序列化的dict信息加载到文件中。这将作为原始dict对象加载,因此在函数中,您应该期望checkpoint == checkpoint(原始定义)。

在这种情况下,我认为您实际要执行的操作是调用保存为checkpoint.pth的文件上的负载,而第一次调用可能没有必要。

def load_checkpoint(filepath):
    model = torch.load(filepath)
    return model

另一种可能性是嵌套对象必须是调用该对象的对象,然后这只是一个小的调整:

def load_checkpoint(filepath):
    checkpoint = torch.load(filepath)
    model = torch.load_state_dict(checkpoint['state_dict'])
    return model

最可能的问题是您正在调用Network类,该类不包含在检查点字典对象中。

我无法说出实际的课程或课程中的其他细微差别,最简单的解决方案可能是仅使用Checkpoint词典中已有的变量来调用Network类定义,如下所示:

model = Network(checkpoint['input_size'],
                checkpoint['output_size'],
                checkpoint['hidden_layers'],
                checkpoint['epochs'],
                checkpoint['optimizer'],
                checkpoint['class_to_index'])
model.load_state_dict(checkpoint['state_dict'])

return model

检查点字典可能仅具有您期望的值(“ input_size”,“ output_size”等),但这只是我所看到的最明显的问题。

相关问题