Wrong implementation of Attention in Pytorch examples

username: Shisho_Sama
cooked: <p>Hi everyone,<br>
I recently tried to implement the attention mechanism in Pytorch. I searched lots of github repos and also the official pytorch implementation <a href="https://pytorch.org/tutorials/intermediate/seq2seq_translation_tutorial.html" rel="nofollow noopener">here</a> plus detailed tutorials such as <a href="https://blog.floydhub.com/attention-mechanism/" rel="nofollow noopener">this one on floydhub</a>. However, it seems to me all of them have implemented the attention mechanism incorrectly!</p>
<p>The problem that I see, is that, in the papers (both of them both, bahdanaus and luoungs), the score is calculated by comparing all encoders hidden states with a single timestep in the decoder, the cuurent/previous timestep to be exact.</p>
<p>The score is calculated and then used along the way so ultimately <strong>the current timestep in the decoder can produce one output</strong> .</p>
<p>Now if you look at the codes, you can see, all of them used an lstm/gru layer instead of a lstm/gru cell where each timestep is exposed!</p>
<p>They simply define the attention mechanism as a module and in its foward() pass, they simply feed the input and what not to the lstm/gru layer and get its outputs! and then use that as the hidden states and multiply/etc it with the encoders states and carry on the rest of the formula.</p>
<p>The idea was to prior to creating <strong>an output in each timestep in the decoder</strong> , <strong>its</strong> current/previous hidden state be used. This is not the case here, the lstm layer simply just does its job, produces all outputs <strong>normally</strong> and we are are using these hiddenstates <strong>in each iteration(as apposed to in each sample)</strong> .</p>
<p>The hidden states were supposed to belong to one sample(one german sentence/paragraph to one english sentence/parageraph) . what happens here is we are using the hidden states from previous iterations that has nothing to do with the current sample! (its as if for translating the current sentence, I’m looking at the hidden state for previous <strong>sentence</strong> (as if for translating the sentence : hey buddy whats up?! I look into the previous sample sentence, mama mia beat the hell out of his son Jose!)</p>
<p>Can someone please explain whats wrong here? am I missing something here?</p>

username: tom
cooked: <p>What seems to happen is that (at least in the tutorial) the functions doing the decoding are only taking one time step (and for GRU output == new hidden) and the loop over the outputs is in the train function. One might argue that it’d be more PyTorchy to wrap this loop into a Module. Using a cell or a model capable of multiple steps but feeding in sequence length 1 isn’t crucial here.</p>
<p>Best regards</p>
<p>Thomas</p>

username: Shisho_Sama
cooked: <p>Thanks a lot. I really appreciate it. I completely forgot about the training, it didnt even occured to me once, they might have done such a thing! Is there any reason that you can think of why they used this while they could easily implement the for loop inside the module ?<br>
Is there any kind of down side for doing that instead of the official example?</p>