ML Community Day is November 9! Join us for updates from TensorFlow, JAX, and more Learn more

tf.foldl

TensorFlow 1 version View source on GitHub

foldl on the list of tensors unpacked from elems on dimension 0. (deprecated argument values)

This foldl operator 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 elems. If initializer is None, elems must contain at least one element, and its first element is used as the initializer.

Suppose that elems is unpacked into values, a list of tensors. The shape of the result tensor is fn(initializer, values[0]).shape`.

This method also allows multi-arity elems and output of fn. If elems is a (possibly nested) list or tuple of tensors, then each of these tensors must have a matching first (unpack) dimension. The signature of fn may match the structure of elems. That is, if elems is (t1, [t2, t3, [t4, t5]]), then an appropriate signature for fn is: fn = lambda (t1, [t2, t3, [t4, t5]]):.

fn The callable to be performed.
elems 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 fn.
initializer (optional) A tensor or (possibly nested) sequence of tensors, as the initial value for the accumulator.
parallel_iterations (optional) The number of iterations allowed to run in parallel.
back_prop (optional) Deprecated. False disables support for back propagation. Prefer using tf.stop_gradient instead.
swap_memory (optional) True enables GPU-CPU memory swapping.
name (optional) Name prefix for the returned tensors.

A tensor or (possibly nested) sequence of tensors, resulting from applying fn consecutively to the list of tensors unpacked from elems, from first to last.

TypeError if fn is not callable.

Example:

elems = tf.constant([1, 2, 3, 4, 5, 6])
sum = foldl(lambda a, x: a + x, elems)
# sum == 21
{"cc_re__cc_re__cc_by_4_0": [" not callable.\n</td>\n</tr>\n</table></p>\n\n<h4 id=\"example\" data-text=\"Example:\">Example:</h4>\n<pre class=\"prettyprint lang-python\" translate=\"no\" dir=\"ltr\"><code translate=\"no\" dir=\"ltr\">elems = tf.constant([1, 2, 3, 4, 5, 6])\nsum = foldl(lambda a, x: a + x, elems)\n# sum == 21\n</code></pre>\n\n    \n\n    \n  </div>\n\n  \n\n  \n    \n    \n      \n       \n    \n    \n  \n\n  \n  \n</article>\n\n\n<devsite-content-footer class=\"nocontent\">\n  <p>Except as otherwise noted, the content of this page is licensed under the <a href=\"https://creativecommons.org/licenses/by/4.0/\">Creative Commons Attribution 4.0 License</a>, and code samples are licensed under the <a href=\"https://www.apache.org/licenses/LICENSE-2.0\">Apache 2.0 License</a>. For details, see the <a href=\"https://developers.google.cn/site-policies\">Google Developers Site Policies</a>. Java is a registered trademark of Oracle and/or its affiliates.</p>\n  <p>Last updated 2020-10-01 UTC.</p>\n</devsite-content-footer>\n\n\n\n\n\n  \n<div class=\"devsite-content-data\">\n  <template class=\"devsite-thumb-rating-down-categ"]}