|View source on GitHub|
scan on the list of tensors unpacked from
elems on dimension 0. (deprecated argument values)
tf.scan( fn, elems, initializer=None, parallel_iterations=10, back_prop=True, swap_memory=False, infer_shape=True, reverse=False, name=None )
The simplest version of
scan repeatedly applies the callable
fn to a sequence of elements from first to last. The elements are made of the tensors unpacked from
elems on dimension 0. The callable fn takes two tensors as arguments. The first argument is the accumulated value computed from the preceding invocation of fn, and the second is the value at the current position of
initializer is None,
elems must contain at least one element, and its first element is used as the initializer.
elems is unpacked into
values, a list of tensors. The shape of the result tensor is
[len(values)] + fn(initializer, values).shape. If reverse=True, it's fn(initializer, values[-1]).shape.
This method also allows multi-arity
elems and accumulator. If
elems is a (possibly nested) list or tuple of tensors, then each of these tensors must have a matching first (unpack) dimension. The second argument of
fn must match the structure of
initializer is provided, the output structure and dtypes of
fn are assumed to be the same as its input; and in this case, the first argument of
fn must match the structure of
initializer is provided, then the output of
fn must have the same structure as
initializer; and the first argument of
fn must match this structure.
For example, if
(t1, [t2, t3]) and
[i1, i2] then an appropriate signature for
fn = lambda (acc_p1, acc_p2), (t1, [t2, t3]): and
fn must return a list,
[acc_n1, acc_n2]. An alternative correct signature for
fn, and the one that works in
fn = lambda a, t:, where
t correspond to the input tuples.
| || The callable to be performed. It accepts two arguments. The first will have the same structure as |
| || A tensor or (possibly nested) sequence of tensors, each of which will be unpacked along their first dimension. The nested sequence of the resulting slices will be the first argument to |
| || (optional) A tensor or (possibly nested) sequence of tensors, initial value for the accumulator, and the expected output type of |
| ||(optional) The number of iterations allowed to run in parallel.|
| || (optional) Deprecated. False disables support for back propagation. Prefer using |
| ||(optional) True enables GPU-CPU memory swapping.|
| ||(optional) False disables tests for consistent output shapes.|
| ||(optional) True scans the tensor last to first (instead of first to last).|
| ||(optional) Name prefix for the returned tensors.|
| A tensor or (possibly nested) sequence of tensors. Each tensor packs the results of applying |
| || if |
| || if the lengths of the output of |
elems = np.array([1, 2, 3, 4, 5, 6]) sum = scan(lambda a, x: a + x, elems) # sum == [1, 3, 6, 10, 15, 21] sum = scan(lambda a, x: a + x, elems, reverse=True) # sum == [21, 20, 18, 15, 11, 6]
elems = np.array([1, 2, 3, 4, 5, 6]) initializer = np.array(0) sum_one = scan( lambda a, x: x - x + a, (elems + 1, elems), initializer) # sum_one == [1, 2, 3, 4, 5, 6]
elems = np.array([1, 0, 0, 0, 0, 0]) initializer = (np.array(0), np.array(1)) fibonaccis = scan(lambda a, _: (a, a + a), elems, initializer) # fibonaccis == ([1, 1, 2, 3, 5, 8], [1, 2, 3, 5, 8, 13])
© 2020 The TensorFlow Authors. All rights reserved.
Licensed under the Creative Commons Attribution License 3.0.
Code samples licensed under the Apache 2.0 License.