mindspore.nn.Moments

class mindspore.nn.Moments(axis=None, keep_dims=None)[source]

Calculate the mean and variance of the input x along the specified axis.

Parameters
  • axis (Union[int, tuple(int)]) – Calculates the mean and variance along the specified axis. When the value is None, it means to calculate the mean and variance of all values of x. Default: None.

  • keep_dims (bool) – If True, the calculation result will retain the dimension of axis, and the dimensions of the mean and variance are the same as the input. If False or None, the dimension of axis will be reduced. Default: None.

Inputs:
  • x (Tensor) - Tensor of any dimension used to calculate the mean and variance. Only float16 and float32 are supported.

Outputs:
  • mean (Tensor) - The mean value of x on axis, with the same data type as input x.

  • variance (Tensor) - The variance of x on axis, with the same data type as input x.

Raises
  • TypeError – If axis is not one of int, tuple, None.

  • TypeError – If keep_dims is neither bool nor None.

  • TypeError – If dtype of x is neither float16 nor float32.

Supported Platforms:

Ascend GPU CPU

Examples

>>> # case1: axis = 0, keep_dims=True
>>> x = Tensor(np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]]), mindspore.float32)
>>> net = nn.Moments(axis=0, keep_dims=True)
>>> output = net(x)
>>> print(output)
(Tensor(shape=[1, 2, 2], dtype=Float32, value=
[[[ 3.00000000e+00, 4.00000000e+00],
  [ 5.00000000e+00, 6.00000000e+00]]]), Tensor(shape=[1, 2, 2], dtype=Float32, value=
[[[ 4.00000000e+00, 4.00000000e+00],
  [ 4.00000000e+00, 4.00000000e+00]]]))
>>> # case2: axis = 1, keep_dims=True
>>> net = nn.Moments(axis=1, keep_dims=True)
>>> output = net(x)
>>> print(output)
(Tensor(shape=[2, 1, 2], dtype=Float32, value=
[[[ 2.00000000e+00, 3.00000000e+00]],
 [[ 6.00000000e+00, 7.00000000e+00]]]), Tensor(shape=[2, 1, 2], dtype=Float32, value=
[[[ 1.00000000e+00, 1.00000000e+00]],
 [[ 1.00000000e+00, 1.00000000e+00]]]))
>>> # case3: axis = 2, keep_dims=None(default)
>>> net = nn.Moments(axis=2)
>>> output = net(x)
>>> print(output)
(Tensor(shape=[2, 2], dtype=Float32, value=
[[ 1.50000000e+00, 3.50000000e+00],
 [ 5.50000000e+00, 7.50000000e+00]]), Tensor(shape=[2, 2], dtype=Float32, value=
[[ 2.50000000e-01, 2.50000000e-01],
 [ 2.50000000e-01, 2.50000000e-01]]))
>>> # case4: axis = None(default), keep_dims=None(default)
>>> net = nn.Moments()
>>> output = net(x)
>>> print(output)
(Tensor(shape=[], dtype=Float32, value= 4.5), Tensor(shape=[], dtype=Float32, value= 5.25))