pytorch学习笔记——加载checkpoint时,程序报错,存在GPU和CPU不同步的情况

当我们需要加载之前训练的checkpoint的时候,有时候会发现之前能训练的代码无法继续训练。

这时候很有可能加载优化器的步骤在加载模型前面,这样可能会导致优化器的参数仍然在CPU上,因此代码需要由原来改为:

#将:
optimizer, scheduler = self.get_optimizers(num_training_steps=total_step)
model = self.model
model.to(self.args.device)

#改为:
model = self.model
model.to(self.args.device)
optimizer, scheduler = self.get_optimizers(num_training_steps=total_step)

 

posted @ 2022-03-24 14:37  ISGuXing  阅读(294)  评论(0编辑  收藏  举报