如何在加载检查点时修复 AttributeError?
How can I fix an AttributeError while loading checkpoint?
我正在研究 Udacity 课程的项目 2(人工智能与 Python 编程)。
我训练了一个模型并将其保存在 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'
我想成功加载检查点。
谢谢
更新:有了完整的代码,我认为问题出在实现上。 torch.load 会将已经反序列化的dict中的信息加载到文件中。这作为原始 dict 对象加载,因此在函数中,您应该期望 checkpoint == checkpoint(original definition).
在这种情况下,我认为您实际上想要做的是调用保存为 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
最有可能的问题是您正在调用网络 class,它不包含在检查点字典对象中。
我无法谈论实际课程或课程中的其他细微差别,最简单的解决方案可能是使用检查点字典中已有的变量调用网络 class 定义,如下所示:
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' 等)但这只是我看到的最明显的问题。
我正在研究 Udacity 课程的项目 2(人工智能与 Python 编程)。
我训练了一个模型并将其保存在 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'
我想成功加载检查点。
谢谢
更新:有了完整的代码,我认为问题出在实现上。 torch.load 会将已经反序列化的dict中的信息加载到文件中。这作为原始 dict 对象加载,因此在函数中,您应该期望 checkpoint == checkpoint(original definition).
在这种情况下,我认为您实际上想要做的是调用保存为 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
最有可能的问题是您正在调用网络 class,它不包含在检查点字典对象中。
我无法谈论实际课程或课程中的其他细微差别,最简单的解决方案可能是使用检查点字典中已有的变量调用网络 class 定义,如下所示:
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' 等)但这只是我看到的最明显的问题。