Regarding GRU training for seq-to-seq models (autoencoder)

bmahjour: <p>I am learning how to build a sequence-to-sequence model from a variety of sources and many of them show the input to a GRU unit to be an iteration over a sequence, for instance a series of one-hot encoded words in a sentence, alongside a hidden state that updates at each pass. <a href="https://pytorch.org/tutorials/intermediate/seq2seq_translation_tutorial.html" rel="noopener nofollow ugc">Here is one of the tutorials I am referring to.</a></p>
<p>I am trying to replicate this in batch training but I am a bit confused by the documentation for GRUs. Given that we train RNNs by iterating through each token in a sequence, when would the sequence length ever be greater than one?</p>
<p>My data is fashioned such that batches are fed into the encoder in chunks [batch_size, maximum_token_length, num_features/one_hot_labels], and having batch_first = True on the GRUs.</p>
<p>Given this, what would be the difference between these two blocks of code?</p>
<pre><code class="lang-auto">for i in range(max_input_length):
    latent, hidden = encoder(this_batch[:,i,:].reshape(batch_size,1, feature_size), hidden)

#vs

latent, hidden = encoder(this_batch, hidden)
</code></pre>
<p>Does the GRU just iterate through the sequence as I would with the for loop? Clearly this is the case when comparing output values from both methods. Why use one versus another?</p>
<p>Here is my yet-so-far unsuccessful training loop for an autoencoder with some notes to my thought process. The loss decreases incredibly slowly despite running on a GPU with a batch size of 32.<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/3/9/39ec77c3368e394eb9177b6b05d4eaa0cc04c28b.png" data-download-href="https://discuss.pytorch.org/uploads/default/39ec77c3368e394eb9177b6b05d4eaa0cc04c28b" title="image"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/3/9/39ec77c3368e394eb9177b6b05d4eaa0cc04c28b_2_690x488.png" alt="image" data-base62-sha1="8gpQrd7Fgn9dmdcV7dJC0ZD4eDN" width="690" height="488" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/3/9/39ec77c3368e394eb9177b6b05d4eaa0cc04c28b_2_690x488.png, https://discuss.pytorch.org/uploads/default/optimized/3X/3/9/39ec77c3368e394eb9177b6b05d4eaa0cc04c28b_2_1035x732.png 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/3/9/39ec77c3368e394eb9177b6b05d4eaa0cc04c28b_2_1380x976.png 2x" data-dominant-color="181A1A"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">image</span><span class="informations">2034×1440 431 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>
<p>Am I fundamentally misunderstanding here or am I being impatient with training time? I’m getting meaningless reconstructions of sequences. I’m not sure if passing the latent representation from the encoder into the decoder at each time step is correct, and I am presuming the blank initialized hidden state would represent the  token.</p>
<p>Any thoughts or help would be greatly appreciated! Thanks a lot.</p>

vdw: <p>Hi! I can offer at least some couple of thoughts:</p>
<ul>
<li>
<p>I have a working implementation of <a href="https://github.com/chrisvdweth/ml-toolkit/blob/master/pytorch/models/text/autoencoder/textrnnae.py" rel="noopener nofollow ugc">RNN-based autoencoder</a> you can have a look. It’s a bit verbose since I looked into support of a Variational Autoencoder which require to flatten the hidden state of the encoder. If you encoder and decoder have the same architecture (i.e., the hidden state have the same shape), then you can copy the last hidden state of the encoder over as the first hidden state of the decoder.</p>
</li>
<li>
<p>I’m a bit confused why you have a <code>decoder.initHidden()</code> call in your code. In the bases Seq2Seq setup the hidden state of the decoder gets directly initialized with the last hidden state of the decoder. I’m also not really sure what <code>latent</code> as output of you encoder is, particularly compared to <code>hidden</code> (in <code>latent, hidden = encoder(...)</code>)</p>
</li>
<li>
<p>You have 2 <code>reshape()</code> calls. Make sure that these calls do exactly what you intend them to do. Sure, they fix your dimensions but they are likely to mess up your data. I’ve wrote a more detailed <a href="https://discuss.pytorch.org/t/for-beginners-do-not-use-view-or-reshape-to-swap-dimensions-of-tensors/75524">forum post</a> on this since it’s such a common occurrence. Without really checking it, my money is that your <code>reshape()</code> call are wrong :).</p>
</li>
<li>
<p>From my experience, an Autoencoder for text can be difficult to train – an Variational Autoencoder even more so. I have so many epoch where the loss goes down slowly until at some point something “snaps in” in the loss goes down more significantly.</p>
</li>
</ul>
<p>I hope that helps a bit.</p>
<p><strong>EDIT:</strong> I was just seeing this sentence: “I’m not sure if passing the latent representation from the encoder into the decoder at each time step is correct, and I am presuming the blank initialized hidden state would represent the token.”</p>
<p>Again, the basic setup is that your input runs through the encoder. This gives you the last hidden state, which you then use as initial hidden state of the decoder. There is no passing of hidden states between the encoder and decoder at each time step. At least not in the basic encoder-decoder setup – I’m sure there are some more advanced techniques that do more fancy stuff. I’ve added below a couple of my lecture slides to visualize the idea.</p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/5/4/5484848bf9c120def685fd5502303d19f6d092a6.png" data-download-href="https://discuss.pytorch.org/uploads/default/5484848bf9c120def685fd5502303d19f6d092a6" title="Screenshot from 2022-10-22 09-17-26"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/5/4/5484848bf9c120def685fd5502303d19f6d092a6_2_690x386.png" alt="Screenshot from 2022-10-22 09-17-26" data-base62-sha1="c3G1A3YgPsP2qWxXOVjfNW8xutM" width="690" height="386" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/5/4/5484848bf9c120def685fd5502303d19f6d092a6_2_690x386.png, https://discuss.pytorch.org/uploads/default/optimized/3X/5/4/5484848bf9c120def685fd5502303d19f6d092a6_2_1035x579.png 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/5/4/5484848bf9c120def685fd5502303d19f6d092a6.png 2x" data-dominant-color="F8F9F9"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">Screenshot from 2022-10-22 09-17-26</span><span class="informations">1079×604 67.2 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div><br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/b/8/b851b60f2c7987a887d4e771199e0817036f16fd.png" data-download-href="https://discuss.pytorch.org/uploads/default/b851b60f2c7987a887d4e771199e0817036f16fd" title="Screenshot from 2022-10-22 09-17-37"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/b/8/b851b60f2c7987a887d4e771199e0817036f16fd_2_690x385.png" alt="Screenshot from 2022-10-22 09-17-37" data-base62-sha1="qiyV2eeXW7PLAmn37c6TNewNMEl" width="690" height="385" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/b/8/b851b60f2c7987a887d4e771199e0817036f16fd_2_690x385.png, https://discuss.pytorch.org/uploads/default/optimized/3X/b/8/b851b60f2c7987a887d4e771199e0817036f16fd_2_1035x577.png 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/b/8/b851b60f2c7987a887d4e771199e0817036f16fd.png 2x" data-dominant-color="EFECE1"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">Screenshot from 2022-10-22 09-17-37</span><span class="informations">1085×606 67.9 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div><br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/5/c/5cd3bde04fff70f1bb42082960935f63025356d3.png" data-download-href="https://discuss.pytorch.org/uploads/default/5cd3bde04fff70f1bb42082960935f63025356d3" title="Screenshot from 2022-10-22 09-17-50"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/5/c/5cd3bde04fff70f1bb42082960935f63025356d3_2_690x387.png" alt="Screenshot from 2022-10-22 09-17-50" data-base62-sha1="dfbA4oODQomdzFMmIgnyMJQyvQL" width="690" height="387" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/5/c/5cd3bde04fff70f1bb42082960935f63025356d3_2_690x387.png, https://discuss.pytorch.org/uploads/default/optimized/3X/5/c/5cd3bde04fff70f1bb42082960935f63025356d3_2_1035x580.png 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/5/c/5cd3bde04fff70f1bb42082960935f63025356d3.png 2x" data-dominant-color="F6F6F7"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">Screenshot from 2022-10-22 09-17-50</span><span class="informations">1091×612 75.1 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>

bmahjour: <p>Thanks so much for the reply!</p>
<p>I kept cracking at it and eventually figured out that the main issue was that I was using the encoder incorrectly. Instead I added a “[BEGIN]” token to each sequence and use that as the initial input and had the hidden state initialized from the latent state from the encoder (I have two outputs from my encoder, the hidden state and a compressed vector of each output of the gpu). I suppose I can use both the latent output vector of the encoder and the final hidden state, or just one as the initial hidden state of the decoder.</p>
<p>Then depending on if there’s teacher forcing or not, the output of the decoder is the next input, along with the new hidden state.</p>
<p>Now I’m getting loss going down quite quickly at first… then it just explodes.</p>
<p>I will have to look closer into my reshape calls. Good catch on that. That might be the main issue now.</p>

vdw: <p>I’m not sure what you mean by latent, as “latent” basically means hidden :).</p>
<p>But again, this all depends on the architecture you implement for the decoder, and this can be anything. I was just referring to the most basic RNN-based encoder-decoder architecture (without attention or anything). As a rule of thumb, the encoder gives some kind of “summary” of the input to the decoder. For the basic architecture that’s simple the last hidden state.</p>

bmahjour: <p>Essentially I have two outputs from the encoder:<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/e/1/e1fef7b257ff8d222a1a6c0e3ed87d623e878f75.jpeg" data-download-href="https://discuss.pytorch.org/uploads/default/e1fef7b257ff8d222a1a6c0e3ed87d623e878f75" title="image"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/e/1/e1fef7b257ff8d222a1a6c0e3ed87d623e878f75_2_690x450.jpeg" alt="image" data-base62-sha1="wffHaJhgi5ymxCpJZp1JFhDzPYF" width="690" height="450" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/e/1/e1fef7b257ff8d222a1a6c0e3ed87d623e878f75_2_690x450.jpeg, https://discuss.pytorch.org/uploads/default/optimized/3X/e/1/e1fef7b257ff8d222a1a6c0e3ed87d623e878f75_2_1035x675.jpeg 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/e/1/e1fef7b257ff8d222a1a6c0e3ed87d623e878f75.jpeg 2x" data-dominant-color="292B2D"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">image</span><span class="informations">1160×758 162 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div><br>
One being the GRU output and the other being the updated hidden vector that’s refed at each iteration. I guess at some point in my education I learned that the compressed bottleneck layer of an autoencoder is the latent space, and the hidden memory state of an RNN being its own separate concept.</p>
<p>That aside, my model is converging but still quite slow (loss from 110 to 80 over 10 hours on a tesla V100… only about 20 epochs though). You were totally right about my reshape calls being incorrect, and after checking all data flows everything is functioning as intended. At this point I feel that the training loop is correct and now I’m just trying to speed this up since my goal is to have a working autoencoder and eventually VAE in a tractable amount of time.</p>
<p>I noticed in your code you have options for bidirectionality and an embedding layer. I think I will next add an embedding layer since that seems to help a lot, then add an attention to create a Transformer model. Is there more I can do to keep improving the model? Can I train on multiple GPUs? It’s hard to get a sense as to whether I just have to truly train for days or if I’m just doing this inefficiently, but I can’t find good “time” benchmarks online anywhere.  Should I make my batch size as large as my GPU can handle?</p>
<p>Sorry for the rapidfire questions, I feel that I’m real close now! Thanks again!!!</p>

vdw: <p>OK, you <code>latent</code> is the <code>output</code> for the GRU layer pushed through a linear layer. Well, <code>output</code> contains <strong>all</strong> hidden states over the whole sequence. I’m not quite sure how you utilize it since the shape of <code>output</code> is <code>(batch_size, seq_len, num_directions*hidden_dim)</code> (ok, <code>num_directions=1</code> in your case).</p>
<p>So you give each decoder time step some information about the whole input sequence. This in some kind of attention. See the slides below; I think you’re doing something like this (not the same though!):</p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/b/b/bbf6cb8038ff73fc78e92297a583ecdbd6f3c5a3.png" data-download-href="https://discuss.pytorch.org/uploads/default/bbf6cb8038ff73fc78e92297a583ecdbd6f3c5a3" title="Screenshot from 2022-10-25 09-22-36"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/b/b/bbf6cb8038ff73fc78e92297a583ecdbd6f3c5a3_2_690x390.png" alt="Screenshot from 2022-10-25 09-22-36" data-base62-sha1="qOO2DZJtMAuA6msgRIxPVDp7VqX" width="690" height="390" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/b/b/bbf6cb8038ff73fc78e92297a583ecdbd6f3c5a3_2_690x390.png, https://discuss.pytorch.org/uploads/default/optimized/3X/b/b/bbf6cb8038ff73fc78e92297a583ecdbd6f3c5a3_2_1035x585.png 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/b/b/bbf6cb8038ff73fc78e92297a583ecdbd6f3c5a3.png 2x" data-dominant-color="F8F9FA"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">Screenshot from 2022-10-25 09-22-36</span><span class="informations">1218×690 61.8 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div><br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/5/f/5feff7ed0626e84479343e5ff04cf64702708494.png" data-download-href="https://discuss.pytorch.org/uploads/default/5feff7ed0626e84479343e5ff04cf64702708494" title="Screenshot from 2022-10-25 09-22-47"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/5/f/5feff7ed0626e84479343e5ff04cf64702708494_2_690x389.png" alt="Screenshot from 2022-10-25 09-22-47" data-base62-sha1="dGHumAFsIBB3XelopENfaxtb60A" width="690" height="389" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/5/f/5feff7ed0626e84479343e5ff04cf64702708494_2_690x389.png, https://discuss.pytorch.org/uploads/default/optimized/3X/5/f/5feff7ed0626e84479343e5ff04cf64702708494_2_1035x583.png 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/5/f/5feff7ed0626e84479343e5ff04cf64702708494.png 2x" data-dominant-color="F6F3F4"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">Screenshot from 2022-10-25 09-22-47</span><span class="informations">1221×690 139 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>