class_docstr: Tests for nn.weighted_moments.
  
  Note that this test inherits from MomentsTest, inheriting all its
  test methods!
  
  It modifies MomentsTest in two ways:
  
  a) By overriding _unweighted_moments, all the codepaths in
     MomentsTest are executed, but with calls to tf.nn.moments()
     replaced by calls to tf.nn.weighted_moments() with a constant
     weight of 1.
  
  b) By overriding RunMomentTest and RunMomentTestWithDynamicShape,
     this test adds multiple additional calls to
     RunWeightedMomentsTest() to exercise correctness with
     non-constant weights and varying broadcasting situations. (It
     also continues to call MomentsTest.Run(Weighted)?MomentsTest as
     well.)
class_docstr_tok: ["Tests","for","nn.weighted_moments",".","Note","that","this","test","inherits","from","MomentsTest",",","inheriting","all","its","test","methods","!","It","modifies","MomentsTest","in","two","ways",":","a",")","By","overriding","_unweighted_moments",",","all","the","codepaths","in","MomentsTest","are","executed",",","but","with","calls","to","tf.nn.moments","(",")","replaced","by","calls","to","tf.nn.weighted_moments","(",")","with","a","constant","weight","of","1.","b",")","By","overriding","RunMomentTest","and","RunMomentTestWithDynamicShape",",","this","test","adds","multiple","additional","calls","to","RunWeightedMomentsTest","(",")","to","exercise","correctness","with","non-constant","weights","and","varying","broadcasting","situations",".","(","It","also","continues","to","call","MomentsTest.Run","(","Weighted",")","?","MomentsTest","as","well",".",")"]
code: def RunWeightedMomentTest(
      self,
      shape,
      weights_shape,
      axes,
      keep_dims,
      dtype,
      dynshapes=False,
  ):
      with self.cached_session() as s:
          x_numpy = np.random.normal(size=shape).astype(np.float32)
          weights_numpy = np.absolute(  # weights must be positive
              np.random.normal(size=weights_shape, loc=1.0).astype(
                  np.float32
              )
          )
  
          # Expand the numpy version to higher precision
          x_numpy = x_numpy.astype(np.float128)
          weights_numpy = weights_numpy.astype(np.float128)
  
          x_shape = [None] * len(shape) if dynshapes else shape
          weights_shape = (
              [None] * len(weights_shape)
              if dynshapes
              else weights_shape
          )
  
          x = array_ops.placeholder(dtype, shape=x_shape)
          weights = array_ops.placeholder(dtype, shape=weights_shape)
  
          mean, var = nn_impl.weighted_moments(
              x, axes, weights, keep_dims=keep_dims
          )
  
          ax = tuple(axes)
  
          def _np_weighted_sum(v):
              return np.sum(
                  weights_numpy * v, axis=ax, keepdims=keep_dims
              )
  
          weight_sum = _np_weighted_sum(np.ones_like(x_numpy))
          expected_mean = _np_weighted_sum(x_numpy) / weight_sum
          expected_mean_squared = np.multiply(
              expected_mean, expected_mean
          )
          expected_x_squared = (
              _np_weighted_sum(np.multiply(x_numpy, x_numpy))
              / weight_sum
          )
          expected_variance = expected_x_squared - expected_mean_squared
  
          mean_v, var_v = s.run(
              [mean, var],
              feed_dict={x: x_numpy, weights: weights_numpy},
          )
  
          self.assertAllCloseAccordingToType(expected_mean, mean_v)
          self.assertAllCloseAccordingToType(expected_variance, var_v)
  
imports: import numpy
  from tensorflow.python.framework import constant_op
  from tensorflow.python.framework import dtypes
  from tensorflow.python.framework import ops
  from tensorflow.python.framework import test_util
  from tensorflow.python.ops import array_ops
  from tensorflow.python.ops import gen_nn_ops
  from tensorflow.python.ops import gradient_checker
  from tensorflow.python.ops import gradients_impl
  from tensorflow.python.ops import math_ops
  from tensorflow.python.ops import nn_impl
  import tensorflow.python.ops.nn_grad
  from tensorflow.python.platform import test
label_desc: Use image node_id 4 for calling the WeightedMomentsTest obj's underlying member method code with example usage: obj.RunWeightedMomentTest(shape, weights_shape, axes, keep_dims, dtype, dynshapes) and returns: np