Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

optim.optimizer.state_dict.state not found when loading from checkpoint #38

Closed
Chung-I opened this issue Apr 25, 2017 · 4 comments
Closed

Comments

@Chung-I
Copy link

Chung-I commented Apr 25, 2017

I encountered an error when using Adam optimizer and resuming training from checkpoint, which states that state in Adam optimizer is not found. I found that the line optim.set_parameters(model.parameters()) wipe out states in
optim.optimizer.state_dict.state.

if not opt.train_from_state_dict and not opt.train_from:
        for p in model.parameters():
            p.data.uniform_(-opt.param_init, opt.param_init)
        encoder.load_pretrained_vectors(opt)
        decoder.load_pretrained_vectors(opt)
        optim = onmt.Optim(
            opt.optim, opt.learning_rate, opt.max_grad_norm,
            lr_decay=opt.learning_rate_decay,
            start_decay_at=opt.start_decay_at
        )
    else:
        print('Loading optimizer from checkpoint:')
        optim = checkpoint['optim']
        print(optim)

    optim.set_parameters(model.parameters())

after I move the line optim.set_parameters(model.parameters()) into the block under if statement, the code works fine:

if not opt.train_from_state_dict and not opt.train_from:
        for p in model.parameters():
            p.data.uniform_(-opt.param_init, opt.param_init)
        encoder.load_pretrained_vectors(opt)
        decoder.load_pretrained_vectors(opt)
        optim = onmt.Optim(
            opt.optim, opt.learning_rate, opt.max_grad_norm,
            lr_decay=opt.learning_rate_decay,
            start_decay_at=opt.start_decay_at
        )
        optim.set_parameters(model.parameters())
    else:
        print('Loading optimizer from checkpoint:')
        optim = checkpoint['optim']
        print(optim)
@ylhsieh
Copy link

ylhsieh commented May 23, 2017

Thanks for your very helpful tip!

@Chung-I
Copy link
Author

Chung-I commented May 24, 2017

@ylhsieh But I later found that the code above is just silencing the error rather than fixing it ...... Adam stop updating parameters when reading from a checkpoint.

@ylhsieh
Copy link

ylhsieh commented May 24, 2017

Thanks again! That is exactly what I was going to post just now. Wonder if anyone has a fix...

@srush
Copy link
Contributor

srush commented Aug 27, 2017

(Superceded by more recent issue)

@srush srush closed this as completed Aug 27, 2017
marcotcr pushed a commit to marcotcr/OpenNMT-py that referenced this issue Sep 20, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants