title: Exporting a model with tf.map_fn
body: <p>I have some tensorflow model which I need to export in a saved model. Below is the simplified code of the model, which I am trying to export.</p>
  
  <pre><code>import tensorflow as tf
  
  
  def foo(x):
    return tf.reduce_sum(x)
  
  
  inputs = tf.keras.layers.Input(shape=(128,128,3))
  y = tf.keras.layers.Conv2D(filters=32, kernel_size=3, padding='SAME')(inputs)
  y = tf.keras.layers.ReLU()(y)
  
  outputs = tf.map_fn(foo, y, dtype=(tf.float32))
  
  model = tf.keras.models.Model(inputs=inputs, outputs=outputs)
  model.save('./export', save_format='tf')
  </code></pre>
  
  <p>but while exporting the model I am getting the following error.</p>
  
  <pre><code>/Users/bruce/.venv/bin/python /Users/bruce/test_project/mymodel/test.py
  Traceback (most recent call last):
    File "/Users/bruce/test_project/mymodel/test.py", line 12, in &lt;module&gt;
      outputs = tf.map_fn(foo, y, dtype=(tf.float32))
    File "/Users/bruce/.venv/lib/python3.6/site-packages/tensorflow_core/python/ops/map_fn.py", line 228, in map_fn
      for elem in elems_flat]
    File "/Users/bruce/.venv/lib/python3.6/site-packages/tensorflow_core/python/ops/map_fn.py", line 228, in &lt;listcomp&gt;
      for elem in elems_flat]
    File "/Users/bruce/.venv/lib/python3.6/site-packages/tensorflow_core/python/ops/tensor_array_ops.py", line 1078, in __init__
      name=name)
    File "/Users/bruce/.venv/lib/python3.6/site-packages/tensorflow_core/python/ops/tensor_array_ops.py", line 716, in __init__
      self._tensor_array = [None for _ in range(size)]
  TypeError: 'Tensor' object cannot be interpreted as an integer
  </code></pre>
  
  <p>I cannot remove the part <code>tf.map_fn</code> which is doing some essential processing which I need in the saved model while deploying it.</p>