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

How to save model for Neural Machine Translation ? #10

Open
wannaphong opened this issue Jul 22, 2018 · 1 comment
Open

How to save model for Neural Machine Translation ? #10

wannaphong opened this issue Jul 22, 2018 · 1 comment

Comments

@wannaphong
Copy link

I want to save model for Neural Machine Translation (https://nbviewer.jupyter.org/github/DSKSD/DeepNLP-models-Pytorch/blob/master/notebooks/07.Neural-Machine-Translation-with-Attention.ipynb). Can you help me ?

@simonjisu
Copy link

you can find how to save your model in pytorch documentation.
SAVING AND LOADING MODELS

In that notebook, insert theses code after model trained.

torch.save({
            'encoder_state_dict': encoder.state_dict(),
            'decoder_state_dict': decoder.state_dict(),
            }, YOUR_PATH)

When loading model, run following codes

encoder = Encoder(...)   # set same parameters as you trained
decoder = Decoder(...)  # set same parameters as you trained
checkpoint = torch.load(YOUR_PATH)
encoder.load_state_dict(checkpoint['encoder_state_dict'])
decoder.load_state_dict(checkpoint['decoder_state_dict'])

or, you can save 2 paths to your model

torch.save(encoder.state_dict(), YOUR_ENCODER_PATH)
torch.save(decoder.state_dict(), YOUR_DECODER_PATH)

When loading model, don't forget load both paths.

encoder = Encoder(...)   # set same parameters as you trained
decoder = Decoder(...)  # set same parameters as you trained
encoder.load_state_dict(torch.load(YOUR_ENCODER_PATH))
decoder.load_state_dict(torch.load(YOUR_DECODER_PATH))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants