How to save model for Neural Machine Translation ?
wannaphong opened this issue · 1 comments
wannaphong commented
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 commented
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))