Problems Training a Seq2Seq autoencoder

username: jafioti
cooked: <p>I’ve been trying to build a simple Seq2Seq autoencoder with GRUs. For some reason, the loss goes down but when I test it on new input or even input from the training set, it outputs another part of the training set instead of the input. I think it’s memorizing the train set but I’m not sure why if it only has to pass the input through as the output. Here is my code I’m using, I’ve tried to slim it down as much as possible.</p>
<p>Model File: <a href="https://drive.google.com/open?id=1n1vtnHyZQ0SNW8gJpTAQ8Y1EQTdPndkC" rel="nofollow noopener">https://drive.google.com/open?id=1n1vtnHyZQ0SNW8gJpTAQ8Y1EQTdPndkC</a><br>
Training File (The one that gets ran): <a href="https://drive.google.com/open?id=17xLWgIaTpV8SoOW3VT-1iCgt4bjGzhW0" rel="nofollow noopener">https://drive.google.com/open?id=17xLWgIaTpV8SoOW3VT-1iCgt4bjGzhW0</a><br>
Data Preperation File (Helper file to handle data): <a href="https://drive.google.com/open?id=16UAq1SBGtirLDeCmr1fYy0az4EON3Hui" rel="nofollow noopener">https://drive.google.com/open?id=16UAq1SBGtirLDeCmr1fYy0az4EON3Hui</a><br>
Data: <a href="https://drive.google.com/open?id=1tNuDJpQvAG-kDfpk3qaivC7jjKk_Ok9-" rel="nofollow noopener">https://drive.google.com/open?id=1tNuDJpQvAG-kDfpk3qaivC7jjKk_Ok9-</a></p>

username: vdw
cooked: <p>That the output is another sentence from the training data is very odd. Something garbled would just mean that the training fails somewhere. But you really get a proper sentences as output, just not the input one.</p>
<p>Did you try to iteratively build the model. Any Seq2Seq model for machine translation can be used as an autoencoder, only that input and target are the same sequence. So one can start with the basic <a href="https://pytorch.org/tutorials/intermediate/seq2seq_translation_tutorial.html" rel="nofollow noopener">PyTorch Seq2Seq</a> tutorial. If this is training fine, one can introduce additional bottelneck layers between the encoder and decoder to see how it’s affecting the training – or even extend it to a Variational Autoencoder.</p>
<p>Can you overtrain the model in a small training dataset?</p>
<p>Well, I had a quick look at your code, but it’s far from a minimal example :). Here are just some things I’ve noticed:</p>
<ul>
<li>
<p>In the line <code>hidden = hidden.view(self.n_layers, 1, input_seq.shape[1], -1)[-1]</code>, why is the second parameter a <code>1</code>? This should be <code>2</code> since it represents the number of directions and you use a bidirectional GRU.</p>
</li>
<li>
<p>Why do you need the encoder output <code>encoded</code> in for the decoder? What is the <code>decoder_input = torch.LongTensor([1 for i in range(encoded.shape[1])])...</code> doing. Usually you have to initialize the target sequence with a special start token, e.g., <code>input = torch.LongTensor([[Token.SOS]] * batch_size).to(self.device)</code></p>
</li>
</ul>
<p>You may want to have a look at my code for an <a href="https://github.com/chrisvdweth/ml-toolkit/blob/master/pytorch/models/text/autoencoder/textrnnae.py" rel="nofollow noopener">Autoencoder</a> and <a href="https://github.com/chrisvdweth/ml-toolkit/blob/master/pytorch/models/text/autoencoder/textrnnvae.py" rel="nofollow noopener">Variational Autoencoder</a>. I started with the PyTorch Seq2Seq tutorial, extended it to the AE, and then to the VAE.</p>

username: jafioti
cooked: <p>First off thank you for the great answer. I am very confused as well because not only are the results not garbled and coherent, but they are exact sentences from other examples. I tried to overtrain the model, and it works on a tiny dataset of 5 examples but when I bring it up to 10 examples the same effect starts to occur.</p>
<p>As for your first question, I want to concatenate the hidden states together from both directions and pass them through a linear to scale them down to one hidden state size to pass to the decoder.</p>
<p>For the second question, I don’t need the encoder output and I don’t use it. The decoder_input is set initially to be the start token * the batch size, though your way is more elegant. I’m pretty sure it does the same thing though.</p>
<p>I am in the process of going through your example, will let you know how it goes.</p>
<p>Thanks for all the help!</p>

username: jafioti
cooked: <p>I’ve finally got it working, and all I needed was to change the encoder forward function. Turns out the problem was in fact the line where I was trying to concatenate the hidden state of the encoder. I’ve replaced it with your flatten function and it works great. Thanks again!</p>