模型训练----断点训练

当我们发现模型训练过程中异常,不得不停止,这是还像继续上次的训练就得都用到断点训练,保存模型的优化器,迭代器,模型,轮数等参数

保存模型,cfg为我的配置文件

checkpoint = {
            'epoch':epoch,
            'model':model.state_dict() if not cfg.more_gpu else model.module.state_dict(),
            'optimizer':optimizer.state_dict(),
            'lr_schedule':scheduler.state_dict(),
            'best_acc':best_acc}
torch.save(checkpoint,cfg.checkpoint_path)

在训练前判断是否继续训练,若是则读取上一次参数,加载全部参数或者变换学习率等等

注释语句为断点训练更改参数,你觉得上次训练学习率过大就可以调小,配合日志功能来调整

start_epoch = -1
best_acc = 0.0
if cfg.resume:
    path_checkpoint = cfg.checkpoint_path
    checkpoint = torch.load(path_checkpoint)
    start_epoch = checkpoint['epoch']
    model.load_state_dict(checkpoint['model']) if not cfg.more_gpu else model.module.load_state_dict(checkpoint['model'])
    model.model.load_state_dict(checkpoint['model'])
    optimizer.load_state_dict(checkpoint['optimizer'])
    #optimizer = torch.optim.__dict__[cfg.optimizer_type](model.parameters(),lr = 0.0001,weight_decay = cfg.wd)
    scheduler.load_state_dict(checkpoint['lr_schedule'])
    #scheduler=lr_scheduler.ExponentialLR(optimizer=optimizer,gamma=0.9)
    best_acc = checkpoint['best_acc']

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

雪豹不会梦到瑞克5

妈妈生的

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值