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

Function weighted_moments

tensorflow/python/ops/nn_impl.py:1372–1446  ·  view source on GitHub ↗

Returns the frequency-weighted mean and variance of `x`. Args: x: A tensor. axes: 1-d tensor of int32 values; these are the axes along which to compute mean and variance. frequency_weights: A tensor of positive weights which can be broadcast with x. name: Name used to

(x, axes, frequency_weights, name=None, keep_dims=None,
                     keepdims=None)

Source from the content-addressed store, hash-verified

1370
1371@tf_export(v1=["nn.weighted_moments"])
1372def weighted_moments(x, axes, frequency_weights, name=None, keep_dims=None,
1373 keepdims=None):
1374 """Returns the frequency-weighted mean and variance of `x`.
1375
1376 Args:
1377 x: A tensor.
1378 axes: 1-d tensor of int32 values; these are the axes along which
1379 to compute mean and variance.
1380 frequency_weights: A tensor of positive weights which can be
1381 broadcast with x.
1382 name: Name used to scope the operation.
1383 keep_dims: Produce moments with the same dimensionality as the input.
1384 keepdims: Alias of keep_dims.
1385
1386 Returns:
1387 Two tensors: `weighted_mean` and `weighted_variance`.
1388 """
1389 keep_dims = deprecated_argument_lookup(
1390 "keepdims", keepdims, "keep_dims", keep_dims)
1391 if keep_dims is None:
1392 keep_dims = False
1393 with ops.name_scope(name, "weighted_moments", [x, frequency_weights, axes]):
1394 x = ops.convert_to_tensor(x, name="x")
1395 frequency_weights = ops.convert_to_tensor(
1396 frequency_weights, name="frequency_weights")
1397
1398 # Unlike moments(), this just uses a simpler two-pass method.
1399
1400 # See comment in moments() WRT precision; it applies here too.
1401 needs_cast = x.dtype == dtypes.float16
1402 if needs_cast:
1403 x = math_ops.cast(x, dtypes.float32)
1404
1405 if frequency_weights.dtype != x.dtype:
1406 frequency_weights = math_ops.cast(frequency_weights, x.dtype)
1407
1408 # Note that we use keep_dims=True for our reductions regardless of the arg;
1409 # this is so that the results remain broadcast-compatible with the inputs.
1410 weighted_input_sum = math_ops.reduce_sum(
1411 frequency_weights * x, axes, name="weighted_input_sum", keepdims=True)
1412
1413 # The shape of the weights isn't necessarily the same as x's
1414 # shape, just broadcast-compatible with it -- so this expression
1415 # performs broadcasting to give a per-item weight, with the same
1416 # shape as (freqency_weights * x). This avoids having to reason
1417 # through all the broadcast logic to compute a correct
1418 # sum_of_weights.
1419 broadcasted_weights = frequency_weights + array_ops.zeros_like(x)
1420
1421 sum_of_weights = math_ops.reduce_sum(
1422 broadcasted_weights, axes, name="sum_of_weights", keepdims=True)
1423
1424 divisor = math_ops.reciprocal(sum_of_weights, name="inv_weight_sum")
1425
1426 weighted_mean = math_ops.multiply(weighted_input_sum, divisor)
1427
1428 # Have the weighted mean; now on to variance:
1429 weighted_distsq = math_ops.reduce_sum(

Callers 1

weighted_moments_v2Function · 0.85

Calls 5

reduce_sumMethod · 0.80
multiplyMethod · 0.80
name_scopeMethod · 0.45
castMethod · 0.45

Tested by

no test coverage detected