map on the list of tensors unpacked from `elems` on dimension 0. The simplest version of `map_fn` repeatedly applies the callable `fn` to a sequence of elements from first to last. The elements are made of the tensors unpacked from `elems`. `dtype` is the data type of the return value of `f
(fn, elems, dtype=None, parallel_iterations=None, back_prop=True,
swap_memory=False, infer_shape=True, name=None)
| 36 | |
| 37 | @tf_export("map_fn") |
| 38 | def map_fn(fn, elems, dtype=None, parallel_iterations=None, back_prop=True, |
| 39 | swap_memory=False, infer_shape=True, name=None): |
| 40 | """map on the list of tensors unpacked from `elems` on dimension 0. |
| 41 | |
| 42 | The simplest version of `map_fn` repeatedly applies the callable `fn` to a |
| 43 | sequence of elements from first to last. The elements are made of the |
| 44 | tensors unpacked from `elems`. `dtype` is the data type of the return |
| 45 | value of `fn`. Users must provide `dtype` if it is different from |
| 46 | the data type of `elems`. |
| 47 | |
| 48 | Suppose that `elems` is unpacked into `values`, a list of tensors. The shape |
| 49 | of the result tensor is `[values.shape[0]] + fn(values[0]).shape`. |
| 50 | |
| 51 | This method also allows multi-arity `elems` and output of `fn`. If `elems` |
| 52 | is a (possibly nested) list or tuple of tensors, then each of these tensors |
| 53 | must have a matching first (unpack) dimension. The signature of `fn` may |
| 54 | match the structure of `elems`. That is, if `elems` is |
| 55 | `(t1, [t2, t3, [t4, t5]])`, then an appropriate signature for `fn` is: |
| 56 | `fn = lambda (t1, [t2, t3, [t4, t5]]):`. |
| 57 | |
| 58 | Furthermore, `fn` may emit a different structure than its input. For example, |
| 59 | `fn` may look like: `fn = lambda t1: return (t1 + 1, t1 - 1)`. In this case, |
| 60 | the `dtype` parameter is not optional: `dtype` must be a type or (possibly |
| 61 | nested) tuple of types matching the output of `fn`. |
| 62 | |
| 63 | To apply a functional operation to the nonzero elements of a SparseTensor |
| 64 | one of the following methods is recommended. First, if the function is |
| 65 | expressible as TensorFlow ops, use |
| 66 | |
| 67 | ```python |
| 68 | result = SparseTensor(input.indices, fn(input.values), input.dense_shape) |
| 69 | ``` |
| 70 | |
| 71 | If, however, the function is not expressible as a TensorFlow op, then use |
| 72 | |
| 73 | ```python |
| 74 | result = SparseTensor( |
| 75 | input.indices, map_fn(fn, input.values), input.dense_shape) |
| 76 | ``` |
| 77 | |
| 78 | instead. |
| 79 | |
| 80 | When executing eagerly, map_fn does not execute in parallel even if |
| 81 | `parallel_iterations` is set to a value > 1. You can still get the |
| 82 | performance benefits of running a function in parallel by using the |
| 83 | `tf.contrib.eager.defun` decorator, |
| 84 | |
| 85 | ```python |
| 86 | # Assume the function being used in map_fn is fn. |
| 87 | # To ensure map_fn calls fn in parallel, use the defun decorator. |
| 88 | @tf.contrib.eager.defun |
| 89 | def func(tensor): |
| 90 | return tf.map_fn(fn, tensor) |
| 91 | ``` |
| 92 | |
| 93 | Note that if you use the defun decorator, any non-TensorFlow Python code |
| 94 | that you may have written in your function won't get executed. See |
| 95 | `tf.contrib.eager.defun` for more details. The recommendation would be to |
no test coverage detected