title: How to get Tensorflow's batch_sequences_with_states to infer sequence lengths automatically
body: <p>I'm trying to use Tensorflow's <a href="https://www.tensorflow.org/api_docs/python/tf/contrib/training/batch_sequences_with_states" rel="nofollow noreferrer">tf.contrib.training.batch_sequences_with_states</a> to prepare data for an RNN, specifically batching and segmenting variable length sequences.</p>
  
  <p>The code below is a toy example that I expect to run without error:</p>
  
  <pre><code>import os
  import tensorflow as tf
  
  BATCH_SIZE = 10
  STATE_SIZE = 5
  STEP_SIZE = 20
  
  input_key = tf.placeholder(tf.string, [])
  
  input_sequences = {
      "inputs": tf.placeholder(tf.float32, [None]),
      "labels": tf.placeholder(tf.float32, [None])
  }
  
  input_context = {
      "length": tf.placeholder(tf.int32)
  }
  
  initial_states = {
      "cell": tf.zeros([STATE_SIZE], tf.float32)
  }
  
  # https://www.tensorflow.org/api_docs/python/tf/contrib/training/batch_sequences_with_states
  batch = tf.contrib.training.batch_sequences_with_states(
      input_key=input_key,
      input_sequences=input_sequences,
      input_context=input_context,
      input_length=None, # infer lengths
      initial_states=initial_states,
      num_unroll=STEP_SIZE,
      batch_size=BATCH_SIZE
  )
  
  init = tf.global_variables_initializer()
  
  sess = tf.Session()
  sess.run(init)
  
  tf.train.start_queue_runners(sess=sess)
  </code></pre>
  
  <p>I'm not feeding any actual data or running anything (other than to initialize the variables). All I am doing is starting the queue runner. I expect this example to run successfully (albeit not doing anything). However, when I run this an exception is thrown:</p>
  
  <pre><code>2017-08-08 01:06:00.789556: E tensorflow/core/common_runtime/executor.cc:644] Executor failed to create kernel. Invalid argument: Shape [-1] has negative dimensions
     [[Node: input_sequences_inputs = Placeholder[dtype=DT_FLOAT, shape=[?], _device="/job:localhost/replica:0/task:0/cpu:0"]()]]
  2017-08-08 01:06:00.796772: E tensorflow/core/common_runtime/executor.cc:644] Executor failed to create kernel. Invalid argument: Shape [-1] has negative dimensions
     [[Node: input_sequences_inputs = Placeholder[dtype=DT_FLOAT, shape=[?], _device="/job:localhost/replica:0/task:0/cpu:0"]()]]
  2017-08-08 01:06:00.798030: E tensorflow/core/common_runtime/executor.cc:644] Executor failed to create kernel. Invalid argument: Shape [-1] has negative dimensions
     [[Node: input_sequences_inputs = Placeholder[dtype=DT_FLOAT, shape=[?], _device="/job:localhost/replica:0/task:0/cpu:0"]()]]
  2017-08-08 01:06:00.805150: E tensorflow/core/common_runtime/executor.cc:644] Executor failed to create kernel. Invalid argument: Shape [-1] has negative dimensions
     [[Node: input_sequences_inputs = Placeholder[dtype=DT_FLOAT, shape=[?], _device="/job:localhost/replica:0/task:0/cpu:0"]()]]
  2017-08-08 01:06:00.810993: E tensorflow/core/common_runtime/executor.cc:644] Executor failed to create kernel. Invalid argument: Shape [-1] has negative dimensions
     [[Node: input_sequences_inputs = Placeholder[dtype=DT_FLOAT, shape=[?], _device="/job:localhost/replica:0/task:0/cpu:0"]()]]
  2017-08-08 01:06:00.812120: E tensorflow/core/common_runtime/executor.cc:644] Executor failed to create kernel. Invalid argument: Shape [-1] has negative dimensions
     [[Node: input_sequences_inputs = Placeholder[dtype=DT_FLOAT, shape=[?], _device="/job:localhost/replica:0/task:0/cpu:0"]()]]
  2017-08-08 01:06:00.818859: E tensorflow/core/common_runtime/executor.cc:644] Executor failed to create kernel. Invalid argument: Shape [-1] has negative dimensions
     [[Node: input_sequences_inputs = Placeholder[dtype=DT_FLOAT, shape=[?], _device="/job:localhost/replica:0/task:0/cpu:0"]()]]
  2017-08-08 01:06:00.822800: E tensorflow/core/common_runtime/executor.cc:644] Executor failed to create kernel. Invalid argument: Shape [-1] has negative dimensions
     [[Node: input_sequences_inputs = Placeholder[dtype=DT_FLOAT, shape=[?], _device="/job:localhost/replica:0/task:0/cpu:0"]()]]
  2017-08-08 01:06:00.823670: E tensorflow/core/common_runtime/executor.cc:644] Executor failed to create kernel. Invalid argument: Shape [-1] has negative dimensions
     [[Node: input_sequences_inputs = Placeholder[dtype=DT_FLOAT, shape=[?], _device="/job:localhost/replica:0/task:0/cpu:0"]()]]
  </code></pre>
  
  <p>My intention is to provide the <code>input_sequences</code> as a placeholder and provide the actual values during training. The sequences will be variable length and so I do not want to define a fixed size for this placeholder, hence the shape of <code>[None]</code> for <code>input_sequences["inputs"]</code> and <code>input_sequences["labels"]</code>.</p>
  
  <p>The documentation suggests that you can provide <code>input_length=None</code> and lengths for the sequences in <code>input_sequences["inputs"]</code> will be inferred from the actual sequences:</p>
  
  <blockquote>
    <p><code>input_length</code> is a Tensor scalar or an int recording the time dimension prior to padding. It should be between 0 and the time dimension. One reason we want to keep track of it is so that we can take it into consideration when computing the loss. If <code>pad=True</code> then <code>input_length</code> can be <code>None</code> and will be inferred.</p>
    
    <p><a href="https://www.tensorflow.org/api_docs/python/tf/contrib/training/batch_sequences_with_states" rel="nofollow noreferrer">https://www.tensorflow.org/api_docs/python/tf/contrib/training/batch_sequences_with_states</a></p>
  </blockquote>
  
  <p>What am I doing wrong?</p>
  
  <p>I am using Python 3.6.1, Tensorflow 1.2.1.</p>