Can I leak my previous hidden state into the current training batch?

tryingtolearnpytorch: <p>Hi there,</p>
<p>I was following this tutorial online for predicting time series:</p>
<aside class="onebox allowlistedgeneric">
  <header class="source">
      <img src="https://stackabuse.com/favicon.png" class="site-icon" width="16" height="16">
      <a href="https://stackabuse.com/time-series-prediction-using-lstm-with-pytorch-in-python/" target="_blank" rel="nofollow noopener">Stack Abuse</a>
  </header>
  <article class="onebox-body">
    <img src="" class="thumbnail" width="16" height="16">

<h3><a href="https://stackabuse.com/time-series-prediction-using-lstm-with-pytorch-in-python/" target="_blank" rel="nofollow noopener">Time Series Prediction using LSTM with PyTorch in Python</a></h3>

<p>Time series data, as the name suggests is a type of data that changes with time.
For instance, the temperature in a 24-hour time period, the price of various
products in a month, the stock prices of a particular company in a year.
Advanced deep...</p>


  </article>
  <div class="onebox-metadata">
    
    
  </div>
  <div style="clear: both"></div>
</aside>

<p>I don’t like how the hidden state is reset to zero for each training batch, as surely that is getting rid of important information about the previous state of the system? Would it be wrong to use the output (the final hidden state), as the initial hidden state for the next training batch in a time series? My project involves a sort of Kalman filter, so in that context, I feel like it would make sense to keep using the previous state given an initial hidden state.</p>

googlebot: <aside class="quote no-group" data-username="tryingtolearnpytorch" data-post="1" data-topic="92799">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/tryingtolearnpytorch/48/12628_2.png" class="avatar"> tryingtolearnpytorch:</div>
<blockquote>
<p>I don’t like how the hidden state is reset to zero for each training batch, as surely that is getting rid of important information about the previous state of the system?</p>
</blockquote>
</aside>
<p>No, that state encodes “present &amp; future” of timeseries in that batch, state transition rules are encoded in rnn module’s parameters.</p>
<p>New batch usually implies a new bunch of timeseries (or at least non-consecutive [random] input slices), thus old momentary states are not applicable. Instead, it is possible, for some tasks, to use static per timeseries features to initialize hidden states (via encoder MLP).</p>

tryingtolearnpytorch: <p>Thanks for your reply! So what would be the way to ecode a previous state like in Kalman Filters? Essentially what I want is a Kalman filter but with highly non-linear functions to model the transition and the emission matrices (in the linear Kalman Filter)</p>

googlebot: <p>Not sure I understand correctly, with rnns you automatically have some encoded non-zero states once you do at least one step. Gated rnns can adapt to treat the first step (or hidden state of zeros) as more important, so they are usable without [non-zero] initial states. If you have multiple timeseries with different a priori states, you can train an encoder network to produce them from some metadata.</p>