Computes the sum of 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. I
(input_tensor, axis=None, keepdims=False, name=None)
| 1519 | @tf_export("math.reduce_sum", "reduce_sum", v1=[]) |
| 1520 | @dispatch.add_dispatch_support |
| 1521 | def reduce_sum(input_tensor, axis=None, keepdims=False, name=None): |
| 1522 | """Computes the sum of elements across dimensions of a tensor. |
| 1523 | |
| 1524 | Reduces `input_tensor` along the dimensions given in `axis`. |
| 1525 | Unless `keepdims` is true, the rank of the tensor is reduced by 1 for each |
| 1526 | entry in `axis`. If `keepdims` is true, the reduced dimensions |
| 1527 | are retained with length 1. |
| 1528 | |
| 1529 | If `axis` is None, all dimensions are reduced, and a |
| 1530 | tensor with a single element is returned. |
| 1531 | |
| 1532 | For example: |
| 1533 | |
| 1534 | ```python |
| 1535 | x = tf.constant([[1, 1, 1], [1, 1, 1]]) |
| 1536 | tf.reduce_sum(x) # 6 |
| 1537 | tf.reduce_sum(x, 0) # [2, 2, 2] |
| 1538 | tf.reduce_sum(x, 1) # [3, 3] |
| 1539 | tf.reduce_sum(x, 1, keepdims=True) # [[3], [3]] |
| 1540 | tf.reduce_sum(x, [0, 1]) # 6 |
| 1541 | ``` |
| 1542 | |
| 1543 | Args: |
| 1544 | input_tensor: The tensor to reduce. Should have numeric type. |
| 1545 | axis: The dimensions to reduce. If `None` (the default), reduces all |
| 1546 | dimensions. Must be in the range `[-rank(input_tensor), |
| 1547 | rank(input_tensor))`. |
| 1548 | keepdims: If true, retains reduced dimensions with length 1. |
| 1549 | name: A name for the operation (optional). |
| 1550 | |
| 1551 | Returns: |
| 1552 | The reduced tensor, of the same dtype as the input_tensor. |
| 1553 | |
| 1554 | @compatibility(numpy) |
| 1555 | Equivalent to np.sum apart the fact that numpy upcast uint8 and int32 to |
| 1556 | int64 while tensorflow returns the same dtype as the input. |
| 1557 | @end_compatibility |
| 1558 | """ |
| 1559 | keepdims = False if keepdims is None else keepdims |
| 1560 | return _may_reduce_to_scalar( |
| 1561 | keepdims, axis, |
| 1562 | gen_math_ops._sum( |
| 1563 | input_tensor, _ReductionDims(input_tensor, axis), keepdims, |
| 1564 | name=name)) |
| 1565 | |
| 1566 | |
| 1567 | @tf_export("math.reduce_euclidean_norm") |
no test coverage detected