moments

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

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

\[\begin{cases} \mathrm{E}[x] = \frac{1}{n}\sum(x) \\ \mathrm{Var}[x] = \frac{1}{n}\sum(x - \mathrm{E}[x])^{2} \end{cases} \]

axis could be negative or None:

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

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

# If axis is None, reduce as a vector and return scalars
# will be applied to return a scalar result
print(dragon.nn.moments(x))  # mean is 3.5, var is 2.916667

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

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