1、打印梯度

for name, param in model.named_parameters():
    if param.requires_grad:
        if param.grad is not None:
            print("{}, gradient: {}".format(name, param.grad.mean()))
        else:
            print("{} has not gradient".format(name))

 

2、保存模型和加载模型

# 保存模型到 filename
torch.save({
'epoch': epoch + 1, 'model': model.module.state_dict() if args.mGPUs else model.state_dict(), 'optimizer': optimizer.state_dict(), }, filename)
# 从 filename 加载模型
checkpoint = torch.load(filename)
start_epoch = checkpoint['epoch']
checkpoint['model'] = {k: v for k, v in chkpt['model'].items() if model.state_dict()[k].numel() == v.numel()}
model.load_state_dict(checkpoint['model'], strict=False)
optimizer.load_state_dict(checkpoint['optimizer'])

 

相关文章:

  • 2021-08-28
  • 2021-12-05
  • 2021-11-02
  • 2021-04-01
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2023-03-20
  • 2022-01-29
  • 2022-12-23
相关资源
相似解决方案