Why LSTM models do not require labels for each step

nolwen_brosson: <p>Hello,</p>
<p>For time related problems like, for example, stock prediction:</p>
<p>Let’s say we have 300 days of data, 10 features, and one target: the price.</p>
<p>Why, for the training, we only need the price of the 300th day?<br>
I know this is the way LSTM models work, but wouldn’t it be useful to take into account the price of the 299 other days for the model?</p>

jackparrySG: <p>Hi Nolwen,</p>
<p>Because LSTMs produce an output at each timestep, its perfectly reasonable to use all of the price information for training. This then becomes a sequence-to-sequence problem. There are various ways to go about doing this, assuming you’re using an MSE loss function, you could compute the MSE for your LSTM output (passed through a Linear layer so as to reduce dimensionality to the same size as your label, ie. 1) at each timestep h_t, compared with the correct label y_t, then sum the loss before backpropagating, or consider all output timesteps as a single vector, and compute the loss against the full vector of labels. There are many more complex architectures for sequence-to-sequence models, most notably encoder-decoder models, the type commonly used for machine translation.</p>
<p>It is also fine to simply use the final label.</p>

nolwen_brosson: <p>Thanks a lot for your answer.</p>
<p>Just to be sure my problem is well understood.<br>
Let say we have this kind of dataset (A 3d array of shape (nb of elements, number of days, number of features) :</p>
<pre><code class="lang-auto">Day 1   
 
            Feature 1 Feature 2 feature 3       label

data 1
data 2
…
…
Data n





Day 2  
 
            Feature 1 Feature 2 feature 3       label

data 1
data 2
…
…
Data n





Day 3  
 
            Feature 1 Feature 2 feature 3       label

data 1
data 2
…
…
Data n

</code></pre>
<p>For the moment, I train my model based on the labels of day 3, using day1 day 2 and day 3 of features. <strong>Thus, labels of day 1 and day 2 are unused with my classic RNN.</strong></p>
<p>Are you saying that a model like a sequence to sequence would use the labels of day 1 and day 2??</p>

jackparrySG: <p>Hey sorry I’m new to the forums and never recieved a notification when you replied.</p>
<blockquote>
<p>Blockquote<br>
Are you saying that a model like a sequence to sequence would use the labels of day 1 and day 2??</p>
</blockquote>
<p>Yep I believe you have understood correctly. Consider my earlier suggestion of using MSE loss for a vector of labels against a vector of outputs. An LSTM with T inputs will give T outputs. You have as many labels as you have input vectors (also T). Instead of taking only the final LSTM output at time T, you could take all of the outputs, pass them individually through a fully-connected layer to reduce dimensionality, and then compute MSE between the vector of outputs and the vector of labels.</p>
<p>More sophisticated sequence to sequence models such as encoder-decoder models are more well-suited to machine translation tasks, where the output sequence could differ in length to the input sequence. In your case both are length T.</p>

nolwen_brosson: <p>Thanks a lot.</p>
<p>If I understand the idea, and keeping my example with 3 days of data.</p>
<ul>
<li>The LSTM model will have 3 outputs (one for each day)</li>
<li>I should save all the 3 outputs</li>
<li>Then, I should put these 3 outputs in a full connected layer, and train it with the 3 labels?</li>
</ul>
<p>Is it the pipeline you are suggesting?</p>

jackparrySG: <p>No problem <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=9" title=":slight_smile:" class="emoji" alt=":slight_smile:"></p>
<p>So that is about right, but you don’t want to save the outputs and train the fully-connected layer separately, but rather use the fully-connected layer (Linear layer in PyTorch) as a final layer in one full network.</p>
<p>You have T timesteps (‘days’)<br>
your features are of dimension f<br>
your LSTM outputs will be whatever the hidden size of your lstm is, lets call that h<br>
your labels (Y) are dimension o (which is 1)<br>
you need to reduce the dimensionality of each h-dimensional LSTM output to o, for this you use a PyTorch Linear layer with h input dimensions and o output dimensions. In most cases, you can pass a sequence to a Linear layer and it will apply the same linear transformation to all vectors in the sequence. Your LSTM output shape will be [T, h], passing this through a linear layer will give a [T, o] output. Your labels are also [T, o], so it should be fine to use MSE to compute the loss over these vectors. Since the o dimension is just 1, you may have to call .squeeze(-1) on the output before passing it to MSE loss. I’m not exactly sure what shape vectors pytorch MSE expects.</p>
<p>input vector - [T, f]<br>
x1 x2 x3 … xT - f-dimensional inputs to LSTM<br>
LSTM output [T, h]<br>
h1 h2 h3 … hT - h-dimensional outputs of LSTM<br>
Linear layer output [T, o]<br>
l1 l2 l3 … lT - o-dimensional Linear layer output</p>
<p>loss = MSE loss (linear layer output, labels)<br>
backpropagate through the whole network (by calling loss.backward() then optimizer.step())</p>

nolwen_brosson: <p>Thanks a lot! Now I totally get your idea. Still, I thing there is one point for which I have not be very clear I think.</p>
<p>What I want is to predict only the last label. So, let’s say I have T timesteps, I want to use T-1 labels to help predict the Tth label. However, here, I think that what we do is try to predict every labels.<br>
What I want is to use the f features of the T days + T-1 labels in order to predict the Tth price.</p>

jackparrySG: <blockquote>
<p>Blockquote<br>
I want to use T-1 labels to help predict the Tth label</p>
</blockquote>
<p>In theory computing the loss over all of the labels during training should help the model predict the final label at inference time. You can train the model as described above, then for computing accuracy on the valid set, you only consider the final label and the final output of the LSTM.</p>
<p>Alternatively, you could append the t-1th label to the feature vector for xt, and compute the loss for the final LSTM state (or the mean over all LSTM states), passed through the linear layer, with yT. This would mean at t=0 you wouldn’t have a label yt-1, here you could append a 0 (sort of “padding”), or you could simply ignore this input.</p>