Explore Library
Code QuizAdvanced

Resuming Training From a Checkpoint

Spot why a resumed training run restarts from the wrong epoch.

Codepython
def save_checkpoint(model, optimizer, epoch, path):
    torch.save({
        'model': model.state_dict(),
        'optimizer': optimizer.state_dict(),
        'epoch': epoch,
    }, path)

def resume(model, optimizer, path):
    ckpt = torch.load(path)
    model.load_state_dict(ckpt['model'])
    optimizer.load_state_dict(ckpt['optimizer'])
    start_epoch = 0
    return start_epoch

What is the bug in this resume logic?

Watch the code walkthrough

Watch on YouTube →