tf.math.cumulative_logsumexp
Stay organized with collections
Save and categorize content based on your preferences.
Compute the cumulative log-sum-exp of the tensor x
along axis
.
tf.math.cumulative_logsumexp(
x, axis=0, exclusive=False, reverse=False, name=None
)
By default, this op performs an inclusive cumulative log-sum-exp, which means
that the first element of the input is identical to the first element of
the output.
This operation is significantly more numerically stable than the equivalent
tensorflow operation tf.math.log(tf.math.cumsum(tf.math.exp(x)))
, although
computes the same result given infinite numerical precision. However, note
that in some cases, it may be less stable than tf.math.reduce_logsumexp
for a given element, as it applies the "log-sum-exp trick" in a different
way.
More precisely, where tf.math.reduce_logsumexp
uses the following trick:
log(sum(exp(x))) == log(sum(exp(x - max(x)))) + max(x)
it cannot be directly used here as there is no fast way of applying it
to each prefix x[:i]
. Instead, this function implements a prefix
scan using pairwise log-add-exp, which is a commutative and associative
(up to floating point precision) operator:
log_add_exp(x, y) = log(exp(x) + exp(y))
= log(1 + exp(min(x, y) - max(x, y))) + max(x, y)
However, reducing using the above operator leads to a different computation
tree (logs are taken repeatedly instead of only at the end), and the maximum
is only computed pairwise instead of over the entire prefix. In general, this
leads to a different and slightly less precise computation.
Args |
x
|
A Tensor . Must be one of the following types: float16 , float32 ,
float64 .
|
axis
|
A Tensor of type int32 or int64 (default: 0). Must be in the
range [-rank(x), rank(x)) .
|
exclusive
|
If True , perform exclusive cumulative log-sum-exp.
|
reverse
|
If True , performs the cumulative log-sum-exp in the reverse
direction.
|
name
|
A name for the operation (optional).
|
Returns |
A Tensor . Has the same shape and type as x .
|
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.
[[["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 2023-10-06 UTC."],[],[],null,["# tf.math.cumulative_logsumexp\n\n\u003cbr /\u003e\n\n|------------------------------------------------------------------------------------------------------------------------------|\n| [View source on GitHub](https://github.com/tensorflow/tensorflow/blob/v2.13.1/tensorflow/python/ops/math_ops.py#L4430-L4483) |\n\nCompute the cumulative log-sum-exp of the tensor `x` along `axis`.\n\n#### View aliases\n\n\n**Compat aliases for migration**\n\nSee\n[Migration guide](https://www.tensorflow.org/guide/migrate) for\nmore details.\n\n[`tf.compat.v1.math.cumulative_logsumexp`](https://www.tensorflow.org/api_docs/python/tf/math/cumulative_logsumexp)\n\n\u003cbr /\u003e\n\n tf.math.cumulative_logsumexp(\n x, axis=0, exclusive=False, reverse=False, name=None\n )\n\nBy default, this op performs an inclusive cumulative log-sum-exp, which means\nthat the first element of the input is identical to the first element of\nthe output.\n\nThis operation is significantly more numerically stable than the equivalent\ntensorflow operation `tf.math.log(tf.math.cumsum(tf.math.exp(x)))`, although\ncomputes the same result given infinite numerical precision. However, note\nthat in some cases, it may be less stable than [`tf.math.reduce_logsumexp`](../../tf/math/reduce_logsumexp)\nfor a given element, as it applies the \"log-sum-exp trick\" in a different\nway.\n\nMore precisely, where [`tf.math.reduce_logsumexp`](../../tf/math/reduce_logsumexp) uses the following trick: \n\n log(sum(exp(x))) == log(sum(exp(x - max(x)))) + max(x)\n\nit cannot be directly used here as there is no fast way of applying it\nto each prefix `x[:i]`. Instead, this function implements a prefix\nscan using pairwise log-add-exp, which is a commutative and associative\n(up to floating point precision) operator: \n\n log_add_exp(x, y) = log(exp(x) + exp(y))\n = log(1 + exp(min(x, y) - max(x, y))) + max(x, y)\n\nHowever, reducing using the above operator leads to a different computation\ntree (logs are taken repeatedly instead of only at the end), and the maximum\nis only computed pairwise instead of over the entire prefix. In general, this\nleads to a different and slightly less precise computation.\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ---- ||\n|-------------|-------------------------------------------------------------------------------------------------|\n| `x` | A `Tensor`. Must be one of the following types: `float16`, `float32`, `float64`. |\n| `axis` | A `Tensor` of type `int32` or `int64` (default: 0). Must be in the range `[-rank(x), rank(x))`. |\n| `exclusive` | If `True`, perform exclusive cumulative log-sum-exp. |\n| `reverse` | If `True`, performs the cumulative log-sum-exp in the reverse direction. |\n| `name` | A name for the operation (optional). |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Returns ------- ||\n|---|---|\n| A `Tensor`. Has the same shape and type as `x`. ||\n\n\u003cbr /\u003e"]]