MCPcopy Create free account
hub / github.com/MegEngine/MegEngine / sum

Function sum

imperative/python/megengine/functional/math.py:150–208  ·  view source on GitHub ↗

r"""Calculates the sum of tensor elements over a given axis (or axes). Args: inp: input tensor. Should have a numeric data type. axis: axis or axes along which sums must be computed. By default, the sum must be computed over the entire tensor. If a sequen

(
    inp: Tensor,
    axis: Optional[Union[int, Sequence[int]]] = None,
    keepdims: bool = False,
)

Source from the content-addressed store, hash-verified

148
149
150def sum(
151 inp: Tensor,
152 axis: Optional[Union[int, Sequence[int]]] = None,
153 keepdims: bool = False,
154) -> Tensor:
155 r"""Calculates the sum of tensor elements over a given axis (or axes).
156
157 Args:
158 inp: input tensor. Should have a numeric data type.
159 axis: axis or axes along which sums must be computed.
160 By default, the sum must be computed over the entire tensor.
161 If a sequence of integers, sums must be computed over multiple axes.
162 keepdims: if ``True``, the reduced axes (dimensions) must be included in the result as singleton dimensions,
163 and, accordingly, the result must be compatible with the input tensor (see :ref:`broadcasting-rule`).
164 Otherwise, if ``False``, the reduced axes (dimensions) must not be included in the result.
165
166 Returns:
167 if the sum was computed over the entire tensor, a zero-dimensional tensor containing the sum;
168 otherwise, a tensor containing the sums.
169 The returned tensor must have a data type determined by :ref:`dtype-promotion`.
170
171 .. admonition:: Special Cases
172
173 Let ``N`` equal the number of elements over which to compute the sum.
174
175 * If ``N`` is 0, the sum is ``0`` (i.e., the empty sum).
176 * If :math:`x_i` is ``NaN``, the sum is ``NaN`` (i.e., ``NaN`` values propagate).
177
178 .. warning::
179
180 If the accumulator is too small, overflow occurs:
181
182 >>> x = F.ones(128, dtype="int8")
183 >>> F.sum(x)
184 Tensor(-128, dtype=int8, device=xpux:0)
185
186 Examples:
187
188 The sum of an empty tensor is the neutral element 0:
189
190 >>> F.sum(Tensor([]))
191 Tensor(0.0, device=xpux:0)
192
193 Normal case:
194
195 >>> F.sum(Tensor([1, 2, 3]))
196 Tensor(6, dtype=int32, device=xpux:0)
197 >>> F.sum(Tensor([0.5, 1.5]))
198 Tensor(2.0, device=xpux:0)
199
200 Along an axis:
201
202 >>> F.sum(Tensor([[1, 2, 3], [4, 5, 6]]), axis=0)
203 Tensor([5 7 9], dtype=int32, device=xpux:0)
204 >>> F.sum(Tensor([[1, 2, 3], [4, 5, 6]]), axis=1)
205 Tensor([ 6 15], dtype=int32, device=xpux:0)
206
207 """

Callers 15

as_countMethod · 0.85
sum_op_statsFunction · 0.85
_sum_channelMethod · 0.85
_sum_channelMethod · 0.85
_sum_channelMethod · 0.85
__init__Method · 0.85
as_countMethod · 0.85
__init__Method · 0.85
_count_visible_keypointsFunction · 0.85
_pmap_sharding_specFunction · 0.85
meanFunction · 0.85

Calls 1

sumMethod · 0.45

Tested by 5

sum_resultFunction · 0.68
test_ReplacementSamplerFunction · 0.68
test_funcFunction · 0.68
normFunction · 0.68
test_null_gradFunction · 0.68