The difference and use of output and hidden state of an RNN?

username: ankitvad
cooked: <p>Like the title states: What’s the difference in using the Hidden State/Output of the last cell/state?<br>
I have gone through various tutorials and code that utilise RNN’s(both GRU and LSTM) for tasks like Seq2Seq and Text Classification.</p>
<p><code>output, hidden = rnn("...", "...")#rnn = GRU/LSTM.</code></p>
<p>For Seq2Seq/Auto-Encoder etc. the  <code>output</code> of the encoder is ignored and the <code>hidden</code> state of the last cell is used as an input to the decoder. And the Decoder uses the <code>output</code> to predict each word one by one.<br>
However, for the Text Classification task the <code>output</code> of the last cell is used to predict the label after passing it through a feed-forward layer and some activation.</p>
<p>Why is the <code>output</code> of the last cell preferred over the <code>hidden</code> state in Text Classification task? Doesn’t the hidden state represent the whole sentence representation ? Or are they interchangeable?</p>

username: jpeg729
cooked: <p>Generally in an encoder-decoder setting the decoder is initialised using the final hidden state of the encoder. As you say, the idea is that the hidden state should represent the whole sentence.</p>
<p>In a labelling setting the output is generally used. For instance, when labelling parts of speech, for each word input, a part of speech is output. That said, when you are classifying the entire sequence, you don’t want a label for each step of input, you only want a single label at the end, so it seems logical to take the last output.</p>
<p>You could use the last hidden state to produce the label, and it might work marvellously, but then again, if the model uses the hidden state to store its intermediate representations of the sentence, then forcing the last hidden state to correspond to the label might interfere with the use of the hidden state for the intermediate representations.</p>
<p>An interesting experiment would be to take the hidden state and feed it into a linear layer or two in order to predict the sentence label.</p>

username: dimitarsh1
cooked: <p>I was wondering the same as I have seen examples (for text classification) that use the last output (<code>output[-1]</code>) as well as the hidden state. See <code>https://github.com/jiangqy/LSTM-Classification-Pytorch/blob/master/utils/LSTMClassifier.py</code> and <code>https://github.com/prakashpandey9/Text-Classification-Pytorch/blob/master/models/LSTM.py</code>. I have also tried a siamese network with contrastive loss as well as MSE on both the output and the hidden state.</p>
<p>Lessons learned: when I built my lstm classifier I used the output and got accuracy around 98. With the hidden - not that good (don’t remember the scores). But interesting was the Siamese network - with MSE and output - great results. With contrastive and output -very bad results. MSE and hidden - bad results; contrastive and hidden - bad results. So basically using the hidden to compute a similarity score or the output has a huge difference in this scenario. However, it also depends (for my case) on the loss function.</p>
<p>Hope this gives some more clarity.<br>
Cheers,<br>
Dimitar</p>

username: Jibin_Mathew
cooked: <p>The output is the concatenation of the hidden state from every time step, whereas hidden is simply the final hidden state, is this statement true in the context of RNNs/LSTMs</p>

username: Jibin_Mathew
cooked: <p>Should I use output or hidden state to be fed into the fully connected layer to perform text classfication?</p>

username: heroadz
cooked: <p>Is there any explanation about this question?</p>