This foldr operator repeatedly applies the callable fn to a sequence
of elements from last to first. The elements are made of the tensors
unpacked from elems. 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]]):.
Args
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.
Returns
A tensor or (possibly nested) sequence of tensors, resulting from applying
fn consecutively to the list of tensors unpacked from elems, from last
to first.
Raises
TypeError
if fn is not callable.
Example
elems=[1,2,3,4,5,6]sum=tf.foldr(lambdaa,x:a+x,elems)# sum == 21
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2024-04-26 UTC."],[],[],null,["# tf.foldr\n\n\u003cbr /\u003e\n\n|----------------------------------------------------------------------------------------------------------------------------------|\n| [View source on GitHub](https://github.com/tensorflow/tensorflow/blob/v2.16.1/tensorflow/python/ops/functional_ops.py#L358-L432) |\n\nfoldr on the list of tensors unpacked from `elems` on dimension 0. (deprecated argument values) \n\n tf.foldr(\n fn,\n elems,\n initializer=None,\n parallel_iterations=10,\n back_prop=True,\n swap_memory=False,\n name=None\n )\n\n| **Deprecated:** SOME ARGUMENT VALUES ARE DEPRECATED: `(back_prop=False)`. They will be removed in a future version. Instructions for updating: back_prop=False is deprecated. Consider using tf.stop_gradient instead. Instead of: results = tf.foldr(fn, elems, back_prop=False) Use: results = tf.nest.map_structure(tf.stop_gradient, tf.foldr(fn, elems))\n\nThis foldr operator repeatedly applies the callable `fn` to a sequence\nof elements from last to first. The elements are made of the tensors\nunpacked from `elems`. The callable fn takes two tensors as arguments.\nThe first argument is the accumulated value computed from the preceding\ninvocation of fn, and the second is the value at the current position of\n`elems`. If `initializer` is None, `elems` must contain at least one element,\nand its first element is used as the initializer.\n\nSuppose that `elems` is unpacked into `values`, a list of tensors. The shape\nof the result tensor is `fn(initializer, values[0]).shape`.\n\nThis method also allows multi-arity `elems` and output of `fn`. If `elems`\nis a (possibly nested) list or tuple of tensors, then each of these tensors\nmust have a matching first (unpack) dimension. The signature of `fn` may\nmatch the structure of `elems`. That is, if `elems` is\n`(t1, [t2, t3, [t4, t5]])`, then an appropriate signature for `fn` is:\n`fn = lambda (t1, [t2, t3, [t4, t5]]):`.\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ---- ||\n|-----------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| `fn` | The callable to be performed. |\n| `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`. |\n| `initializer` | (optional) A tensor or (possibly nested) sequence of tensors, as the initial value for the accumulator. |\n| `parallel_iterations` | (optional) The number of iterations allowed to run in parallel. |\n| `back_prop` | (optional) Deprecated. False disables support for back propagation. Prefer using [`tf.stop_gradient`](../tf/stop_gradient) instead. |\n| `swap_memory` | (optional) True enables GPU-CPU memory swapping. |\n| `name` | (optional) Name prefix for the returned tensors. |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Returns ------- ||\n|---|---|\n| A tensor or (possibly nested) sequence of tensors, resulting from applying `fn` consecutively to the list of tensors unpacked from `elems`, from last to first. ||\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Raises ------ ||\n|-------------|--------------------------|\n| `TypeError` | if `fn` is not callable. |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Example ------- ||\n|---|---|\n| \u003cbr /\u003e elems = [1, 2, 3, 4, 5, 6] sum = tf.foldr(lambda a, x: a + x, elems) # sum == 21 \u003cbr /\u003e ||\n\n\u003cbr /\u003e"]]