How to save a LSTM Seq2Seq network (encoder and decoder) from example in tutorials section

Thezox: <p>I am using the code from here, but I do not how to <strong>save the mode</strong>l, encoder and decoder so I do not have to train it every time I want to use it <a href="https://pytorch.org/tutorials/intermediate/seq2seq_translation_tutorial.html#training-and-evaluating" rel="nofollow noopener">https://pytorch.org/tutorials/intermediate/seq2seq_translation_tutorial.html#training-and-evaluating</a></p>
<p><strong>This part is crucial for me</strong> - i want to save <strong>encoder1 and attn_decoder1</strong> so I can use them after.<br>
How do I do that?</p>
<pre><code>train_now = "yes"
if train_now == "yes": 
    hidden_size = 256
    print("input_lang.n_words",input_lang.n_words)
    print("input_lang.n_words",input_lang.n_words)
    encoder1 = EncoderRNN(input_lang.n_words, hidden_size).to(device)
    attn_decoder1 = AttnDecoderRNN(hidden_size, output_lang.n_words, dropout_p=0.1).to(device)
    trainIters(encoder1, attn_decoder1, 5000, print_every=50)
    evaluateRandomly(encoder1, attn_decoder1)
else: 
    #load somehow encoder1 and attn_decoder1
    evaluateRandomly(encoder1, attn_decoder1)
######################################################################</code></pre>

JamesTrick: <p>Hi! Check out this <a href="https://pytorch.org/tutorials/beginner/saving_loading_models.html#saving-multiple-models-in-one-file" rel="nofollow noopener">section on saving multiple models in one file.</a></p>

vdw: <p>Saving should be as simple as:</p>
<pre><code>torch.save(encoder1.state_dict(), 'encoder.dict')
torch.save(attn_decoder1.state_dict(), 'decoder.dict')
</code></pre>
<p>And loading:</p>
<pre><code>encoder = EncoderRNN(input_lang.n_words, hidden_size).to(device)
decoder = AttnDecoderRNN(hidden_size, output_lang.n_words, dropout_p=0.1).to(device)

encoder.load_state_dict(torch.load('encoder.dict'))
decoder.load_state_dict(torch.load('decoder.dict'))</code></pre>

Thezox: <p>This works:<br>
train_now = “nyes”<br>
if train_now == “yes”:<br>
hidden_size = 256<br>
print(“input_lang.n_words”,input_lang.n_words)<br>
print(“input_lang.n_words”,input_lang.n_words)<br>
encoder1 = EncoderRNN(input_lang.n_words, hidden_size).to(device)<br>
attn_decoder1 = AttnDecoderRNN(hidden_size, output_lang.n_words, dropout_p=0.1).to(device)<br>
trainIters(encoder1, attn_decoder1, 500, print_every=50)<br>
evaluateRandomly(encoder1, attn_decoder1)<br>
torch.save(encoder1, “encoder1.pt”)<br>
torch.save(attn_decoder1, “attn_decoder1.pt”)<br>
else:<br>
encoder1 = torch.load(“encoder1.pt”)<br>
encoder1.eval()<br>
attn_decoder1 = torch.load(“attn_decoder1.pt”)<br>
attn_decoder1.eval()<br>
<span class="hashtag">#load</span> models<br>
evaluateRandomly(encoder1, attn_decoder1)<br>
######################################################################</p>

vdw: <p>You might want to read <a href="https://pytorch.org/tutorials/beginner/saving_loading_models.html#saving-loading-model-for-inference" rel="nofollow noopener">this article</a>, to compare the differences between saving/loading the entire model (this is what you are doing) and saving/loading the model in terms of its state.</p>

Thezox: <p>I read, as far as i understand, it will not work if I move the model to another directory and load from there. Maybe?</p>

Dammio: <p>I am here just for the correction. I believe this is a RNN Seq-2-Seq network which uses GRU units, not a LTSM Seq2Seq network. GRU units generally take fewer parameters than LSTM, as they do not have an output gate.</p>