moments

dragon.math.moments(
  inputs,
  axis=None,
  keep_dims=False,
  **kwargs
)[source]

Compute the mean and variance of input along the given axes.

\[\begin{cases} \text{mean} = \frac{1}{n}\sum(\text{input}) \\ \text{variance} = \frac{1}{n}\sum(x - \text{mean}(\text{input}))^{2} \end{cases} \]

The argument axis could be negative or None:

x = dragon.constant([[1, 2, 3], [4, 5, 6]])

# A negative axis is the last-k axis
print(dragon.math.moments(x, 1))
print(dragon.math.moments(x, -1))  # Equivalent

# If ``axis`` is None, the vector-style reduction
# will be applied to return a scalar result
print(dragon.math.moments(x))  # Mean is 3.5, Var is 2.916667

# Also, ``axis`` could be a sequence of integers
print(dragon.math.moments(x, [0, 1]))  # Mean is 3.5, Var is 2.916667
Parameters:
  • inputs (dragon.Tensor) – The tensor \(x\).
  • axis (Union[int, Sequence[int]], optional) – The axis to reduce.
  • keep_dims (bool, optional, default=False) – Keep the reduced dimensions or not.
Returns:

  • dragon.Tensor – The mean tensor.
  • dragon.Tensor – The variance tensor.