title: Working with variable-length text in Tensorflow
body: <p>I am building a Tensorflow model to perform inference on text phrases.
  For sake of simplicity, assume I need a classifier with fixed number of output classes but a <em>variable-length text</em> in input. In other words, my mini batch would be a sequence of phrases but not all phrases have the same length. </p>
  
  <pre><code>data = ['hello',
          'my name is Mark',
          'What is your name?']
  </code></pre>
  
  <p>My first preprocessing step was to build a dictionary of all possible words in the dictionary and map each word to its integer word-Id. The input becomes:</p>
  
  <pre><code>data = [[1],
          [2, 3, 4, 5],
          [6, 4, 7, 3]
  </code></pre>
  
  <p>What's the best way to handle this kind of input? Can <a href="https://www.tensorflow.org/versions/r0.9/api_docs/python/io_ops.html#placeholder" rel="noreferrer">tf.placeholder()</a> handle variable-size input within the same batch of data? 
  Or should I pad all strings such that they all have the same length, equal to the length of the longest string, using some placeholder for the missing words? This seems to be very memory inefficient if some string are much longer that most of the others.</p>
  
  <p>-- EDIT --</p>
  
  <p>Here is a concrete example.</p>
  
  <p>When I know the size of my datapoints (and all the datapoint have the same length, eg. 3) I normally use something like:</p>
  
  <pre><code>input = tf.placeholder(tf.int32, shape=(None, 3)
  
  with tf.Session() as sess:
    print(sess.run([...], feed_dict={input:[[1, 2, 3], [1, 2, 3]]}))
  </code></pre>
  
  <p>where the first dimension of the placeholder is the minibatch size.</p>
  
  <p>What if the input sequences are words in sentences of different length?</p>
  
  <pre><code>feed_dict={input:[[1, 2, 3], [1]]}
  </code></pre>