MCPcopy Create free account
hub / github.com/DeepRec-AI/DeepRec / reduce_logsumexp

Function reduce_logsumexp

tensorflow/python/ops/math_ops.py:2465–2518  ·  view source on GitHub ↗

Computes log(sum(exp(elements across dimensions of a tensor))). Reduces `input_tensor` along the dimensions given in `axis`. Unless `keepdims` is true, the rank of the tensor is reduced by 1 for each entry in `axis`. If `keepdims` is true, the reduced dimensions are retained with length 1.

(input_tensor, axis=None, keepdims=False, name=None)

Source from the content-addressed store, hash-verified

2463
2464@tf_export("math.reduce_logsumexp", "reduce_logsumexp", v1=[])
2465def reduce_logsumexp(input_tensor, axis=None, keepdims=False, name=None):
2466 """Computes log(sum(exp(elements across dimensions of a tensor))).
2467
2468 Reduces `input_tensor` along the dimensions given in `axis`.
2469 Unless `keepdims` is true, the rank of the tensor is reduced by 1 for each
2470 entry in `axis`. If `keepdims` is true, the reduced dimensions
2471 are retained with length 1.
2472
2473 If `axis` has no entries, all dimensions are reduced, and a
2474 tensor with a single element is returned.
2475
2476 This function is more numerically stable than log(sum(exp(input))). It avoids
2477 overflows caused by taking the exp of large inputs and underflows caused by
2478 taking the log of small inputs.
2479
2480 For example:
2481
2482 ```python
2483 x = tf.constant([[0., 0., 0.], [0., 0., 0.]])
2484 tf.reduce_logsumexp(x) # log(6)
2485 tf.reduce_logsumexp(x, 0) # [log(2), log(2), log(2)]
2486 tf.reduce_logsumexp(x, 1) # [log(3), log(3)]
2487 tf.reduce_logsumexp(x, 1, keepdims=True) # [[log(3)], [log(3)]]
2488 tf.reduce_logsumexp(x, [0, 1]) # log(6)
2489 ```
2490
2491 Args:
2492 input_tensor: The tensor to reduce. Should have numeric type.
2493 axis: The dimensions to reduce. If `None` (the default), reduces all
2494 dimensions. Must be in the range `[-rank(input_tensor),
2495 rank(input_tensor))`.
2496 keepdims: If true, retains reduced dimensions with length 1.
2497 name: A name for the operation (optional).
2498
2499 Returns:
2500 The reduced tensor.
2501 """
2502 keepdims = False if keepdims is None else keepdims
2503 input_tensor = ops.convert_to_tensor(input_tensor)
2504 with ops.name_scope(name, "ReduceLogSumExp", [input_tensor]) as name:
2505 raw_max = reduce_max(input_tensor, axis=axis, keepdims=True)
2506 my_max = array_ops.stop_gradient(
2507 array_ops.where(
2508 gen_math_ops.is_finite(raw_max), raw_max,
2509 array_ops.zeros_like(raw_max)))
2510 result = gen_math_ops.log(
2511 reduce_sum(
2512 gen_math_ops.exp(gen_math_ops.sub(input_tensor, my_max)),
2513 axis,
2514 keepdims=keepdims))
2515 if not keepdims:
2516 my_max = array_ops.reshape(my_max, array_ops.shape(result))
2517 result = gen_math_ops.add(result, my_max)
2518 return _may_reduce_to_scalar(keepdims, axis, result)
2519
2520
2521@tf_export("linalg.trace", v1=["linalg.trace", "trace"])

Callers 1

reduce_logsumexp_v1Function · 0.85

Calls 9

_may_reduce_to_scalarFunction · 0.85
reshapeMethod · 0.80
reduce_maxFunction · 0.70
reduce_sumFunction · 0.70
name_scopeMethod · 0.45
logMethod · 0.45
subMethod · 0.45
shapeMethod · 0.45
addMethod · 0.45

Tested by

no test coverage detected