title: How to apply a function along an axis which can modify the length of samples?
body: <p>I have a batch of variable-length sequences and three channels. So <code>x</code> has the shape <code>(None, None, 3)</code> or <code>[batch_size, sample_length, channel]</code> with <code>batch_size</code> and <code>sample_length</code> being dynamic dimensions.</p>
  
  <p>In one of my steps I want to apply a function <code>f</code> to <em>each</em> of the samples in <code>x</code>. The problem with <code>f</code>: The <code>sample_length</code> can change. </p>
  
  <p>As I tried to run:</p>
  
  <pre><code>new_x = tf.map_fn(lambda sample: f(sample), x)
  </code></pre>
  
  <p>I got this error:</p>
  
  <pre class="lang-none prettyprint-override"><code>Could not write to TensorArray index 2 because the value shape is 
  [10,257] which is incompatible with the TensorArray's inferred 
  element shape: [11,257] (consider setting infer_shape=False).
  </code></pre>
  
  <p>which makes sense since, as stated, <code>f</code> may modify <code>sample_length</code>.</p>
  
  <p>The question here is how could I make something like this work? Since each sample has a variable length to begin with, I am sure this must somehow be possible but I don't know how I could achieve that.</p>
  
  <p>Any ideas?</p>
  
  <hr>
  
  <h3>Environment</h3>
  
  <pre><code>$ pip freeze | grep tensorflow-gpu
  tensorflow-gpu==1.12.0
  </code></pre>