moments

dragon.vm.tensorflow.nn.moments(
  x,
  axes=None,
  keepdims=False,
  name=None
)[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(\text{input} - \text{mean})^{2} \end{cases} \]

The argument axis could be negative or None:

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

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

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

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

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