title: How to use reset_states(states) function in Keras?
body: <p>I'm trying to set the LSTM internal state before training each batch.
  I'm sharing my test code and findings, hoping to find an answer and help others that are addressing similar problems.</p>
  
  <p>In particular, for each data I have a feature X (which doesn't change over time) and a sequence P = p1, p2, p3,... p30.
  The goal is: given X and p1,p2,p3 predict p4, p5, .. p30.</p>
  
  <p>To this aim, I want to initialize the hidden state of an LSTM with X, as done in several works (e.g., neuraltalk), then the LSTM has to be fit with p1,p2,p3 to predict p4,..,p30.
  This initialization is needed before each batch (batch_size=1), therefore I need to have the control of the LSTM states initialization.
  Considerint this question <a href="https://stackoverflow.com/questions/42415909/initializing-lstm-hidden-state-tensorflow-keras">Initializing LSTM hidden state Tensorflow/Keras</a> I've tested the following code:</p>
  
  <p>First of all I've added some prints in the reset_states() function defined in recurrent.py, in order to understand what exactly happens.</p>
  
  <pre><code>def reset_states(self, states=None):
      if not self.stateful:
          raise AttributeError('Layer must be stateful.')
      batch_size = self.input_spec[0].shape[0]
  
      if not batch_size:
          raise ValueError('If a RNN is stateful, it needs to know '
                           'its batch size. Specify the batch size '
                           'of your input tensors: \n'
                           '- If using a Sequential model, '
                           'specify the batch size by passing '
                           'a `batch_input_shape` '
                           'argument to your first layer.\n'
                           '- If using the functional API, specify '
                           'the time dimension by passing a '
                           '`batch_shape` argument to your Input layer.')
      # initialize state if None
      if self.states[0] is None:
          self.states = [K.zeros((batch_size, self.units))
                         for _ in self.states]
          print "reset states A (all zeros)"  
      elif states is None:
          for state in self.states:
              K.set_value(state, np.zeros((batch_size, self.units)))
          print "reset states B (all zeros)"  
  
      else:
          if not isinstance(states, (list, tuple)):
              states = [states]
              print "reset states C (list or tuple copying)"  
  
          if len(states) != len(self.states):
              raise ValueError('Layer ' + self.name + ' expects ' +
                               str(len(self.states)) + ' states, '
                               'but it received ' + str(len(states)) +
                               ' state values. Input received: ' +
                               str(states))
          for index, (value, state) in enumerate(zip(states, self.states)):
              if value.shape != (batch_size, self.units):
                  raise ValueError('State ' + str(index) +
                                   ' is incompatible with layer ' +
                                   self.name + ': expected shape=' +
                                   str((batch_size, self.units)) +
                                   ', found shape=' + str(value.shape))
              K.set_value(state, value)
              print "reset states D (set values)"                
              print value
              print "\n"
  </code></pre>
  
  <p>Here is the test code:</p>
  
  <pre><code>import tensorflow as tf
  from keras.layers import LSTM
  from keras.layers import Input
  from keras.models import Model
  import numpy as np
  import keras.backend as K
  
  input = Input(batch_shape=(1,3,1))
  lstm_layer = LSTM(10,stateful=True)(input)
  &gt;&gt;&gt; reset states A (all zeros)
  </code></pre>
  
  <p>As you can see, the first print is executed when the lstm layer is created</p>
  
  <pre><code>model = Model(input,lstm_layer)
  model.compile(optimizer="adam", loss="mse")
  
  with tf.Session() as sess:
      tf.global_variables_initializer().run()
      h = sess.run(model.layers[1].states[0])
      c = sess.run(model.layers[1].states[1])
  print h
  &gt;&gt;&gt; [[ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.]]
  print c
  &gt;&gt;&gt; [[ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.]]
  </code></pre>
  
  <p>The internal states have been set to all zeros.
  As an alternative the function reset_states() can be used</p>
  
  <pre><code>model.layers[1].reset_states()
  &gt;&gt;&gt; reset states B (all zeros)
  </code></pre>
  
  <p>The second message has been printed in this case. Everything seem to work correctly.
  Now I want to set the states with arbitrary values.</p>
  
  <pre><code>new_h = K.variable(value=np.ones((1, 10)))
  new_c = K.variable(value=np.ones((1, 10))+1)
  
  model.layers[1].states[0] = new_h
  model.layers[1].states[1] = new_c
  with tf.Session() as sess:
       tf.global_variables_initializer().run()
       h = sess.run(model.layers[1].states[0])
       c = sess.run(model.layers[1].states[1])
  
  print h
  &gt;&gt;&gt; [[ 1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.]]
  print c
  &gt;&gt;&gt; [[ 2.,  2.,  2.,  2.,  2.,  2.,  2.,  2.,  2.,  2.]]
  </code></pre>
  
  <p>Ok, I've successfully set both hidden states with my vectors of all one and all two.
  However, it worth to exploit the class function reset_states() which takes as input the states.
  This function exploits the function K.set_values(x,values) which expects 'values' to be a numpy array.</p>
  
  <pre><code>new_h_5 = np.zeros((1,10))+5
  new_c_24 = np.zeros((1,10))+24
  model.layers[1].reset_states([new_h_5,new_c_24])
  </code></pre>
  
  <p>It seems to work, indeed the output is:</p>
  
  <pre><code>&gt;&gt;&gt; reset states D (set values)
  &gt;&gt;&gt; [[ 5.  5.  5.  5.  5.  5.  5.  5.  5.  5.]]
  &gt;&gt;&gt; 
  &gt;&gt;&gt; 
  &gt;&gt;&gt; 
  &gt;&gt;&gt; 
  &gt;&gt;&gt; reset states D (set values)
  &gt;&gt;&gt; [[ 24.  24.  24.  24.  24.  24.  24.  24.  24.  24.]]
  </code></pre>
  
  <p>However, if i want to check if the states have been initializated I find the previous initialization values (all one, all two).</p>
  
  <pre><code>with tf.Session() as sess:
   tf.global_variables_initializer().run()
   hh = sess.run(model.layers[1].states[0])
   cc = sess.run(model.layers[1].states[1])
  print hh
  &gt;&gt;&gt; [[ 1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.]]
  print cc
  &gt;&gt;&gt; [[ 2.,  2.,  2.,  2.,  2.,  2.,  2.,  2.,  2.,  2.]]
  </code></pre>
  
  <p>What is exactly happening here? Why the function seems working according to the prints but doesn't change the values of the internal states?</p>