LSTM with sequences - Online Training copy true values with 1 day lag

Duonijo: <p>Hello everyone,</p>
<p>I’m encountering issues while working on an LSTM implementation with online training. There are two primary problems:</p>
<ol>
<li><strong>Overfitting during Online Training:</strong></li>
</ol>
<ul>
<li>I can train my model successfully with satisfactory results. However, when simulating online training over a year, the model exhibits significant overfitting, with predicted values (<code>y_pred</code>) mirroring true values (<code>y_true</code>) but with a one-day lag.</li>
</ul>
<ol start="2">
<li><strong>Spikes in Predictions:</strong></li>
</ol>
<ul>
<li>Occasionally, the LSTM produces spikes in predictions. I’ve explored batch normalization as a solution, and while implementing it during the training loop is effective, it fails during online training due to feeding the network with a batch of length 1 at a time.</li>
</ul>
<p>Here’s an overview of my code structure:</p>
<ul>
<li>I have implemented the code as a script with various functions such as <code>split_sequence</code> and <code>online_training</code>.</li>
<li>The architecture involves creating sequences from the input data. For instance, if <code>X</code> has a shape of (2000, 14) with 14 columns/features, applying the sequence function results in a shape of (2000, 7, 14) with 7 as the lookback value.</li>
</ul>
<pre data-code-wrap="python"><code class="lang-python">def split_sequences(input_sequences, output_sequence, n_steps_in, n_steps_out):
    X, y = list(), list()  # instantiate X and y
    for i in range(len(input_sequences)):
        # find the end of the input, output sequence
        end_ix = i + n_steps_in
        out_end_ix = end_ix + n_steps_out - 1
        # check if we are beyond the dataset
        if out_end_ix &gt; len(input_sequences): break
        # gather input and output of the pattern
        seq_x, seq_y = input_sequences[i:end_ix], output_sequence[end_ix - 1:out_end_ix, -1]
        X.append(seq_x), y.append(seq_y)
    return np.array(X), np.array(y)
</code></pre>
<ul>
<li>The data is then split into training, validation, and test tensors, where the test set (<code>X_test</code>) consists of 365 days, and the remaining data is split into an 80/20 ratio for training and validation tensors.</li>
<li>The initial training involves training the model on <code>X_train</code> with early stopping based on the validation set <code>X_val</code>.</li>
<li>To simulate real-world usage, the <code>update_loop</code> function runs the model every day, forecasts for the next 7 days, retrains the model on new data, and repeats the process.</li>
</ul>
<p>Despite setting <code>h_prev</code> and <code>c_prev</code> to <code>None</code> at every step and trying different sequence lengths(even 1 to hide previous days data), the issue persists. Additionally, modifying the loss computation to focus only on the initial values of <code>y_true</code> and <code>y_pred</code> did not yield any improvement.</p>
<p>I’m seeking guidance on resolving the one-day lag issue and understanding why the model is consistently producing almost correct outputs during online training, even with a poorly trained initial model.</p>
<p>Additionally, interested individuals can find the test on my GitHub repository. It’s worth noting that while the dataset provided in the test is not the private dataset I’m currently working with, the same issue arises with the randomly generated dataset used for testing purposes.</p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/b/3/b398ff013b8da290eb68b1b360dd2276d2e4adb3.png" data-download-href="https://discuss.pytorch.org/uploads/default/b398ff013b8da290eb68b1b360dd2276d2e4adb3" title="test"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/b/3/b398ff013b8da290eb68b1b360dd2276d2e4adb3_2_690x492.png" alt="test" data-base62-sha1="pCNg6Wf5scutukBtMUGRRTl7Y3h" width="690" height="492" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/b/3/b398ff013b8da290eb68b1b360dd2276d2e4adb3_2_690x492.png, https://discuss.pytorch.org/uploads/default/optimized/3X/b/3/b398ff013b8da290eb68b1b360dd2276d2e4adb3_2_1035x738.png 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/b/3/b398ff013b8da290eb68b1b360dd2276d2e4adb3_2_1380x984.png 2x" data-dominant-color="F1EFF3"><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">test</span><span class="informations">2100×1500 198 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>You can find the code and illustration on this repository to test my problem :</p><aside class="onebox allowlistedgeneric" data-onebox-src="https://github.com/Duonijo/AI-Forecasting">
  <header class="source">
      <img src="https://github.githubassets.com/favicons/favicon.svg" class="site-icon" width="32" height="32">

      <a href="https://github.com/Duonijo/AI-Forecasting" target="_blank" rel="noopener nofollow ugc">GitHub</a>
  </header>

  <article class="onebox-body">
    <div class="aspect-image" style="--aspect-ratio:690/344;"><img src="https://opengraph.githubassets.com/6b09bd08662adb8e97f352e77326553d49da3e4bb8831e3a576ab0adea924e73/Duonijo/AI-Forecasting" class="thumbnail" width="690" height="345"></div>

<h3><a href="https://github.com/Duonijo/AI-Forecasting" target="_blank" rel="noopener nofollow ugc">GitHub - Duonijo/AI-Forecasting</a></h3>

  <p>Contribute to Duonijo/AI-Forecasting development by creating an account on GitHub.</p>


  </article>

  <div class="onebox-metadata">
    
    
  </div>

  <div style="clear: both"></div>
</aside>

<p>By default it s torch for cpu in requirement.txt do not hesitate to reinstall in your environment torch with cuda to accelerate it.</p>

vdw: <p>I’m not quite sure what you mean by “online training”? How exactly does your batch look like? When you give the shapes – <code>(2000, 14)</code> and <code>(2000, 7, 14)</code> – what are their meanings? I assume 2000 is the batch size, 7 is the sequence lengths, and 14 is the number or features?</p>

Duonijo: <p>Hi,<br>
Sorry i did rewrite my text with better syntax and forgot to rewrite the meaning of 2000.</p>
<p>It s my initial dataset length.</p>
<p>So i do have X of length 2000<br>
And when i do the split i end up with X_test length 365 (last 365 elements of X) and the rest is splitted to X_train and X_val as 80/20.</p>
<p>So I have 3 tensors like X_train(1308, 7, 14), X_val(327, 7 14), X_test(365, 7, 14).<br>
7 is my sequence_length and 14 number of features</p>
<p>By online training i Mean, i do have a initial training over X_train, then i want to update my model weights without training from scratch every day with new input data only.<br>
The good term is “online learning” i think.</p>
<p>That’s what i called online training, when i update my model with lower learning_rate.</p>

Duonijo: <p>Hey, just in case,  I did work on another thing.<br>
I am right back on this project with the same strang behavior.</p>
<p>If anyone has an idea.</p>
<p>Thanks you</p>