title: Why does Keras LSTM batch size used for prediction have to be the same as fitting batch size?
body: <p>When using a Keras LSTM to predict on time series data I've been getting errors when I'm trying to train the model using a batch size of 50, while then trying to predict on the same model using a batch size of 1 (ie just predicting the next value).  </p>
  
  <p>Why am I not able to train and fit the model with multiple batches at once, and then use that model to predict for anything other than the same batch size.  It doesn't seem to make sense, but then I could easily be missing something about this.</p>
  
  <p>Edit:  this is the model.  <code>batch_size</code> is 50, <code>sl</code> is sequence length, which is set at 20 currently.</p>
  
  <pre><code>    model = Sequential()
      model.add(LSTM(1, batch_input_shape=(batch_size, 1, sl), stateful=True))
      model.add(Dense(1))
      model.compile(loss='mean_squared_error', optimizer='adam')
      model.fit(trainX, trainY, epochs=epochs, batch_size=batch_size, verbose=2)
  </code></pre>
  
  <p>here is the line for predicting on the training set for RMSE</p>
  
  <pre><code>    # make predictions
      trainPredict = model.predict(trainX, batch_size=batch_size)
  </code></pre>
  
  <p>here is the actual prediction of unseen time steps</p>
  
  <pre><code>for i in range(test_len):
      print('Prediction %s: ' % str(pred_count))
  
      next_pred_res = np.reshape(next_pred, (next_pred.shape[1], 1, next_pred.shape[0]))
      # make predictions
      forecastPredict = model.predict(next_pred_res, batch_size=1)
      forecastPredictInv = scaler.inverse_transform(forecastPredict)
      forecasts.append(forecastPredictInv)
      next_pred = next_pred[1:]
      next_pred = np.concatenate([next_pred, forecastPredict])
  
      pred_count += 1
  </code></pre>
  
  <p>This issue is with the line:</p>
  
  <p><code>forecastPredict = model.predict(next_pred_res, batch_size=batch_size)</code></p>
  
  <p>The error when batch_size here is set to 1 is:</p>
  
  <p><code>ValueError: Cannot feed value of shape (1, 1, 2) for Tensor 'lstm_1_input:0', which has shape '(10, 1, 2)'</code> which is the same error that throws when <code>batch_size</code> here is set to 50 like the other batch sizes as well.</p>
  
  <p>The total error is:</p>
  
  <pre><code>    forecastPredict = model.predict(next_pred_res, batch_size=1)
    File "/home/entelechy/tf_keras/lib/python3.5/site-packages/keras/models.py", line 899, in predict
      return self.model.predict(x, batch_size=batch_size, verbose=verbose)
    File "/home/entelechy/tf_keras/lib/python3.5/site-packages/keras/engine/training.py", line 1573, in predict
      batch_size=batch_size, verbose=verbose)
     File "/home/entelechy/tf_keras/lib/python3.5/site-packages/keras/engine/training.py", line 1203, in _predict_loop
      batch_outs = f(ins_batch)
    File "/home/entelechy/tf_keras/lib/python3.5/site-packages/keras/backend/tensorflow_backend.py", line 2103, in __call__
      feed_dict=feed_dict)
    File "/home/entelechy/tf_keras/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 767, in run
      run_metadata_ptr)
    File "/home/entelechy/tf_keras/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 944, in _run
      % (np_val.shape, subfeed_t.name, str(subfeed_t.get_shape())))
  ValueError: Cannot feed value of shape (1, 1, 2) for Tensor 'lstm_1_input:0', which has shape '(10, 1, 2)'
  </code></pre>
  
  <p>Edit:  Once I set the model to <code>stateful=False</code> then I am able to use different batch sizes for fitting/training and prediction.  What is the reason for this?</p>