<p>从0.8版起,存在<code>map_fn</code>。从<a href="https://www.tensorflow.org/api_docs/python/functional_ops/higher_order_operators#map_fn" rel="noreferrer">documentation</a>:</p>
<blockquote>
<p>map_fn(fn, elems, dtype=None, parallel_iterations=10, back_prop=True,
swap_memory=False, name=None)</p>
<p>map on the list of tensors unpacked from <code>elems</code> on dimension 0.</p>
<p>This map operator repeatedly applies the callable <code>fn</code> to a sequence of elements from first to last. The elements are made of the tensors unpacked from <code>elems</code>. <code>dtype</code> is the data type of the return value of <code>fn</code>. Users must provide <code>dtype</code> if it is different from the data type of <code>elems</code>.</p>
<p>Suppose that <code>elems</code> is unpacked into <code>values</code>, a list of tensors. The shape of the result tensor is <code>[len(values)] + fn(values[0]).shape</code>.</p>
<p>Args:</p>
<p>fn: The callable to be performed.</p>
<p>elems: A tensor to be unpacked to apply <code>fn</code>.</p>
<p>dtype: (optional) The output type of <code>fn</code>.</p>
<p>parallel_iterations: (optional) The number of iterations allowed to run
in parallel.
back_prop: (optional) True enables back propagation.
swap_memory: (optional) True enables GPU-CPU memory swapping.
name: (optional) Name prefix for the returned tensors.</p>
<p>Returns:</p>
<p>A tensor that packs the results of applying <code>fn</code> to the list of tensors
unpacked from <code>elems</code>, from first to last.</p>
<p>Raises:</p>
<p>TypeError: if <code>fn</code> is not callable.</p>
<p>Example:</p>
</blockquote>
<pre><code> elems = [1, 2, 3, 4, 5, 6]
squares = map_fn(lambda x: x * x, elems)
# squares == [1, 4, 9, 16, 25, 36]
```
</code></pre>