class_docstr: Model for predicting autofills given context.
class_docstr_tok: ["Model","for","predicting","autofills","given","context","."]
docstr: Normalizes batch w/ moving population stats for training, o/w batch.
docstr_tok: ["Normalizes","batch","w\/","moving","population","stats","for","training",",","o\/w","batch","."]
code: def apply_batchnorm(self, x):
      """Normalizes batch w/ moving population stats for training, o/w batch."""
      output_dim = x.get_shape()[-1]
      gammas = tf.get_variable(
          "gamma",
          [1, 1, 1, output_dim],
          initializer=tf.constant_initializer(0.1),
      )
      betas = tf.get_variable(
          "beta", [output_dim], initializer=tf.constant_initializer(0.0)
      )
  
      popmean = tf.get_variable(
          "popmean",
          shape=[1, 1, 1, output_dim],
          trainable=False,
          collections=[
              tf.GraphKeys.MODEL_VARIABLES,
              tf.GraphKeys.GLOBAL_VARIABLES,
          ],
          initializer=tf.constant_initializer(0.0),
      )
      popvariance = tf.get_variable(
          "popvariance",
          shape=[1, 1, 1, output_dim],
          trainable=False,
          collections=[
              tf.GraphKeys.MODEL_VARIABLES,
              tf.GraphKeys.GLOBAL_VARIABLES,
          ],
          initializer=tf.constant_initializer(1.0),
      )
  
      decay = 0.01
      if self.is_training:
          batchmean, batchvariance = tf.nn.moments(
              x, [0, 1, 2], keep_dims=True
          )
          mean, variance = batchmean, batchvariance
          updates = [
              popmean.assign_sub(decay * (popmean - mean)),
              popvariance.assign_sub(decay * (popvariance - variance)),
          ]
          # make update happen when mean/variance are used
          with tf.control_dependencies(updates):
              mean, variance = tf.identity(mean), tf.identity(variance)
          self.popstats_by_batchstat[batchmean] = popmean
          self.popstats_by_batchstat[batchvariance] = popvariance
      else:
          mean, variance = popmean, popvariance
  
      return tf.nn.batch_normalization(
          x,
          mean,
          variance,
          betas,
          gammas,
          self.hparams.batch_norm_variance_epsilon,
      )
  
imports: from __future__ import absolute_import
  from __future__ import division
  from __future__ import print_function
  import collections
  import os
  from magenta.models.coconet import lib_hparams
  from magenta.models.coconet import lib_tfutil
  import tensorflow.compat.v1
label_desc: Use image node_id 21 for calling the CoconetGraph obj's underlying member method code with example usage: obj.apply_batchnorm(x) and returns: tf