title: Proper way to feed time-series data to stateful LSTM?
body: <p>Let's suppose I have a sequence of integers:</p>
  
  <p><code>0,1,2, ..</code></p>
  
  <p>and want to predict the next integer given the last 3 integers, e.g.:</p>
  
  <p><code>[0,1,2]-&gt;5</code>, <code>[3,4,5]-&gt;6</code>, etc</p>
  
  <p>Suppose I setup my model like so:</p>
  
  <pre><code>batch_size=1
  time_steps=3
  model = Sequential()
  model.add(LSTM(4, batch_input_shape=(batch_size, time_steps, 1), stateful=True))
  model.add(Dense(1))
  </code></pre>
  
  <p>It is my understanding that model has the following structure (please excuse the crude drawing):</p>
  
  <p><a href="https://i.stack.imgur.com/Eqenl.png" rel="noreferrer"><img src="https://i.stack.imgur.com/Eqenl.png" alt="enter image description here"></a></p>
  
  <p><strong>First Question: is my understanding correct?</strong></p>
  
  <p>Note I have drawn the previous states <code>C_{t-1}, h_{t-1}</code> entering the picture as this is exposed when specifying <code>stateful=True</code>.  In this simple "next integer prediction" problem, the performance should improve by providing this extra information (as long as the previous state results from the previous 3 integers).</p>
  
  <p><strong>This brings me to my main question:</strong>  It seems the standard practice (for example see this <a href="https://machinelearningmastery.com/time-series-prediction-lstm-recurrent-neural-networks-python-keras/" rel="noreferrer">blog post</a> and the <a href="https://keras.io/preprocessing/sequence/" rel="noreferrer">TimeseriesGenerator</a> keras preprocessing utility), is to feed a staggered set of inputs to the model during training.</p>
  
  <p>For example:</p>
  
  <pre><code>batch0: [[0, 1, 2]]
  batch1: [[1, 2, 3]]
  batch2: [[2, 3, 4]]
  etc
  </code></pre>
  
  <p>This has me confused because it seems this is requires the output of the 1st Lstm Cell (corresponding to the 1st time step).  See this figure:</p>
  
  <p><img src="https://i.stack.imgur.com/lM2RC.png" width="550"></p>
  
  <p>From the tensorflow <a href="https://www.tensorflow.org/api_docs/python/tf/keras/layers/LSTM" rel="noreferrer">docs</a>:</p>
  
  <blockquote>
    <p><strong>stateful</strong>: Boolean (default False). If True, the last state for each
    sample at index i in a batch will be used as initial state for the
    sample of index i in the following batch.</p>
  </blockquote>
  
  <p>it seems this "internal" state isn't available and all that is available is the final state.  See this figure:</p>
  
  <p><img src="https://i.stack.imgur.com/l8cUb.png" width="550"></p>
  
  <p>So, if my understanding is correct (which it's clearly not), shouldn't we be feeding non-overlapped windows of samples to the model when using <code>stateful=True</code>?  E.g.:</p>
  
  <pre><code>batch0: [[0, 1, 2]]
  batch1: [[3, 4, 5]]
  batch2: [[6, 7, 8]]
  etc
  </code></pre>