MCPcopy Create free account
hub / github.com/DeepRec-AI/DeepRec / moments

Function moments

tensorflow/python/ops/nn_impl.py:1271–1332  ·  view source on GitHub ↗

Calculate the mean and variance of `x`. The mean and variance are calculated by aggregating the contents of `x` across `axes`. If `x` is 1-D and `axes = [0]` this is just the mean and variance of a vector. Note: shift is currently not used; the true mean is computed and used. When usin

(
    x,
    axes,
    shift=None,  # pylint: disable=unused-argument
    name=None,
    keep_dims=None,
    keepdims=None)

Source from the content-addressed store, hash-verified

1269
1270@tf_export(v1=["nn.moments"])
1271def moments(
1272 x,
1273 axes,
1274 shift=None, # pylint: disable=unused-argument
1275 name=None,
1276 keep_dims=None,
1277 keepdims=None):
1278 """Calculate the mean and variance of `x`.
1279
1280 The mean and variance are calculated by aggregating the contents of `x`
1281 across `axes`. If `x` is 1-D and `axes = [0]` this is just the mean
1282 and variance of a vector.
1283
1284 Note: shift is currently not used; the true mean is computed and used.
1285
1286 When using these moments for batch normalization (see
1287 `tf.nn.batch_normalization`):
1288
1289 * for so-called "global normalization", used with convolutional filters with
1290 shape `[batch, height, width, depth]`, pass `axes=[0, 1, 2]`.
1291 * for simple batch normalization pass `axes=[0]` (batch only).
1292
1293 Args:
1294 x: A `Tensor`.
1295 axes: Array of ints. Axes along which to compute mean and
1296 variance.
1297 shift: Not used in the current implementation
1298 name: Name used to scope the operations that compute the moments.
1299 keep_dims: produce moments with the same dimensionality as the input.
1300 keepdims: Alias to keep_dims.
1301
1302 Returns:
1303 Two `Tensor` objects: `mean` and `variance`.
1304 """
1305 keep_dims = deprecated_argument_lookup(
1306 "keepdims", keepdims, "keep_dims", keep_dims)
1307 if keep_dims is None:
1308 keep_dims = False
1309 with ops.name_scope(name, "moments", [x, axes]):
1310 # The dynamic range of fp16 is too limited to support the collection of
1311 # sufficient statistics. As a workaround we simply perform the operations
1312 # on 32-bit floats before converting the mean and variance back to fp16
1313 y = math_ops.cast(x, dtypes.float32) if x.dtype == dtypes.float16 else x
1314 # Compute true mean while keeping the dims for proper broadcasting.
1315 mean = math_ops.reduce_mean(y, axes, keepdims=True, name="mean")
1316 # sample variance, not unbiased variance
1317 # Note: stop_gradient does not change the gradient that gets
1318 # backpropagated to the mean from the variance calculation,
1319 # because that gradient is zero
1320 variance = math_ops.reduce_mean(
1321 math_ops.squared_difference(y, array_ops.stop_gradient(mean)),
1322 axes,
1323 keepdims=True,
1324 name="variance")
1325 if not keep_dims:
1326 mean = array_ops.squeeze(mean, axes)
1327 variance = array_ops.squeeze(variance, axes)
1328 if x.dtype == dtypes.float16:

Callers 1

moments_v2Function · 0.85

Calls 4

reduce_meanMethod · 0.80
name_scopeMethod · 0.45
castMethod · 0.45

Tested by

no test coverage detected