tf.compat.v1.foldl
Stay organized with collections
Save and categorize content based on your preferences.
foldl on the list of tensors unpacked from elems
on dimension 0.
tf.compat.v1.foldl(
fn,
elems,
initializer=None,
parallel_iterations=10,
back_prop=True,
swap_memory=False,
name=None
)
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]]):
.
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) True enables support for back propagation.
|
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 first
to last.
|
Raises |
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
|
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates. Some content is licensed under the numpy license.
Last updated 2023-10-06 UTC.
[null,null,["Last updated 2023-10-06 UTC."],[],[],null,["# tf.compat.v1.foldl\n\n\u003cbr /\u003e\n\n|---------------------------------------------------------------------------------------------------------------------------------|\n| [View source on GitHub](https://github.com/tensorflow/tensorflow/blob/v2.14.0/tensorflow/python/ops/functional_ops.py#L42-L158) |\n\nfoldl on the list of tensors unpacked from `elems` on dimension 0. \n\n tf.compat.v1.foldl(\n fn,\n elems,\n initializer=None,\n parallel_iterations=10,\n back_prop=True,\n swap_memory=False,\n name=None\n )\n\nThis foldl operator repeatedly applies the callable `fn` to a sequence\nof elements from first to last. The elements are made of the tensors\nunpacked from `elems` on dimension 0. The callable fn takes two tensors as\narguments. The first argument is the accumulated value computed from the\npreceding invocation of fn, and the second is the value at the current\nposition of `elems`. If `initializer` is None, `elems` must contain at least\none element, and 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) True enables support for back propagation. |\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 first to last. ||\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 = tf.constant([1, 2, 3, 4, 5, 6]) sum = foldl(lambda a, x: a + x, elems) # sum == 21 \u003cbr /\u003e ||\n\n\u003cbr /\u003e"]]