title: How to feed in and retrieve state of LSTM in tensorflow C/ C++
body: <p>I'd like to build and train a multi-layer LSTM model (stateIsTuple=True) in python, and then load and use it in C++. But I'm having a hard time figuring out how to feed and fetch states in C++, mainly because I don't have string names which I can reference. </p>
  
  <p>E.g. I put the initial state in a named scope such as </p>
  
  <pre><code>    with tf.name_scope('rnn_input_state'):
          self.initial_state = cell.zero_state(args.batch_size, tf.float32)
  </code></pre>
  
  <p>and this appears in the graph as below, but how can I feed to these in C++?</p>
  
  <p><a href="https://i.stack.imgur.com/On4a9.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/On4a9.png" alt="enter image description here"></a></p>
  
  <p>Also, how can I fetch the current state in C++? I tried the graph construction code below in python but I'm not sure if it's the right thing to do, because last_state should be a tuple of tensors, not a single tensor (though I can see that the last_state node in tensorboard is 2x2x50x128, which sounds like it just concatenated the states as I have 2 layers, 128 rnn size, 50 mini batch size, and lstm cell - with 2 state vectors).</p>
  
  <pre><code>    with tf.name_scope('outputs'):
          outputs, last_state = legacy_seq2seq.rnn_decoder(inputs, self.initial_state, cell, loop_function=loop if infer else None)
          output = tf.reshape(tf.concat(outputs, 1), [-1, args.rnn_size], name='output')
  </code></pre>
  
  <p>and this is what it looks like in tensorboard</p>
  
  <p><a href="https://i.stack.imgur.com/lBXJT.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/lBXJT.png" alt="enter image description here"></a></p>
  
  <p>Should I concat and split the state tensors so there is only ever one state tensor going in and out? Or is there a better way?</p>
  
  <p>P.S. Ideally the solution won't involve hard-coding the number of layers (or rnn size). So I can just have four strings input_node_name, output_node_name, input_state_name, output_state_name, and the rest is derived from there.</p>