class_docstr: Adapts the inner kernel's `momentum_distribution` to estimated variance.
  
  This kernel uses an online variance estimate to adjust a diagonal covariance
  matrix for each of the state parts. More specifically, the
  `momentum_distribution` of the innermost kernel is set to a diagonal
  multivariate normal distribution whose variance is the *inverse* of the
  online estimate. The inverse of the covariance of the momentum is often called
  the "mass matrix" in the context of Hamiltonian Monte Carlo.
  
  This preconditioning scheme works well when the covariance is diagonally
  dominant, and may give reasonable results even when the number of draws is
  less than the dimension. In particular, it should generally do a better job
  than no preconditioning, which implicitly uses an identity mass matrix.
  
  Note that this kernel does not implement a calibrated sampler; rather, it is
  intended to be used as one step of an iterative adaptation process. It
  should not be used when drawing actual samples.
class_docstr_tok: ["Adapts","the","inner","kernel","'s","`","momentum_distribution","`","to","estimated","variance",".","This","kernel","uses","an","online","variance","estimate","to","adjust","a","diagonal","covariance","matrix","for","each","of","the","state","parts",".","More","specifically",",","the","`","momentum_distribution","`","of","the","innermost","kernel","is","set","to","a","diagonal","multivariate","normal","distribution","whose","variance","is","the","*","inverse","*","of","the","online","estimate",".","The","inverse","of","the","covariance","of","the","momentum","is","often","called","the","``","mass","matrix","''","in","the","context","of","Hamiltonian","Monte","Carlo",".","This","preconditioning","scheme","works","well","when","the","covariance","is","diagonally","dominant",",","and","may","give","reasonable","results","even","when","the","number","of","draws","is","less","than","the","dimension",".","In","particular",",","it","should","generally","do","a","better","job","than","no","preconditioning",",","which","implicitly","uses","an","identity","mass","matrix",".","Note","that","this","kernel","does","not","implement","a","calibrated","sampler",";","rather",",","it","is","intended","to","be","used","as","one","step","of","an","iterative","adaptation","process",".","It","should","not","be","used","when","drawing","actual","samples","."]
code: def one_step(self, current_state, previous_kernel_results, seed=None):
      with tf.name_scope(
          mcmc_util.make_name(
              self.name, "diagonal_mass_matrix_adaptation", "one_step"
          )
      ):
          variance_parts = previous_kernel_results.running_variance
          inner_results = previous_kernel_results.inner_results
  
          # Step the inner kernel.
          inner_kwargs = {} if seed is None else dict(seed=seed)
          new_state, new_inner_results = self.inner_kernel.one_step(
              current_state, inner_results, **inner_kwargs
          )
  
          def update_running_variance():
              diags = [
                  variance_part.variance()
                  for variance_part in variance_parts
              ]
              new_state_parts = tf.nest.flatten(new_state)
              new_variance_parts = []
              for variance_part, diag, state_part in zip(
                  variance_parts, diags, new_state_parts
              ):
                  # Compute new variance for each variance part, accounting for partial
                  # batching of the variance calculation across chains (ie, some, all,
                  # or none of the chains may share the estimated mass matrix).
                  #
                  # For example, say
                  #
                  # state_part has shape       [2, 3, 4] + [5, 6]  (batch + event)
                  # variance_part has shape          [4] + [5, 6]
                  # log_prob has shape         [2, 3, 4]
                  #
                  # i.e., we have a batch of chains of shape [2, 3, 4], and 4 mass
                  # matrices, each being shared across a [2, 3]-batch of chains. Note
                  # this division is inferred from the shapes of the state part, the
                  # log_prob, and the user-provided initial running variances.
                  #
                  # Until RunningVariance supports rank > 1 chunking, we need to flatten
                  # the states that go into updating the variance estimates. In the
                  # above example, `state_part` will be reshaped to `[6, 4, 5, 6]`, and
                  # fed to `RunningVariance.update(state_part, axis=0)`, recording
                  # 6 new observations in the running variance calculation.
                  # `RunningVariance.variance()` will then be of shape `[4, 5, 6]`, and
                  # the resulting momentum distribution will have batch shape of
                  # `[2, 3, 4]` and event_shape of `[5, 6]`, matching the state_part.
                  state_rank = ps.rank(state_part)
                  variance_rank = ps.rank(diag)
                  num_reduce_dims = state_rank - variance_rank
  
                  state_part_shape = ps.shape(state_part)
                  # This reshape adds a 1 when reduce_dims==0, and collapses all the
                  # lead dimensions to a single one otherwise.
                  reshaped_state = ps.reshape(
                      state_part,
                      ps.concat(
                          [
                              [
                                  ps.reduce_prod(
                                      state_part_shape[:num_reduce_dims]
                                  )
                              ],
                              state_part_shape[num_reduce_dims:],
                          ],
                          axis=0,
                      ),
                  )
  
                  # The `axis=0` here removes the leading dimension we got from the
                  # reshape above, so the new_variance_parts have the correct shape
                  # again.
                  new_variance_parts.append(
                      variance_part.update(reshaped_state, axis=0)
                  )
              return new_variance_parts
  
          def update_momentum():
              diags = [
                  variance_part.variance()
                  for variance_part in new_variance_parts
              ]
              # Update the momentum.
              prev_momentum_distribution = (
                  self.momentum_distribution_getter_fn(
                      new_inner_results
                  )
              )
              new_momentum_distribution = (
                  preconditioning_utils.update_momentum_distribution(
                      prev_momentum_distribution, diags
                  )
              )
              updated_new_inner_results = (
                  self.momentum_distribution_setter_fn(
                      new_inner_results, new_momentum_distribution
                  )
              )
              return updated_new_inner_results
  
          step = previous_kernel_results.step + 1
          if self.num_estimation_steps is None:
              new_variance_parts = update_running_variance()
              new_inner_results = update_momentum()
          else:
              new_variance_parts = mcmc_util.choose(
                  step <= previous_kernel_results.num_estimation_steps,
                  update_running_variance(),
                  variance_parts,
              )
              new_inner_results = mcmc_util.choose(
                  tf.equal(
                      step, previous_kernel_results.num_estimation_steps
                  ),
                  update_momentum(),
                  new_inner_results,
              )
          new_kernel_results = previous_kernel_results._replace(
              inner_results=new_inner_results,
              running_variance=new_variance_parts,
              step=step,
          )
  
          return new_state, new_kernel_results
  
imports: import collections
  import tensorflow.compat.v2
  from tensorflow_probability.python.experimental.mcmc import preconditioning_utils
  from tensorflow_probability.python.experimental.stats import sample_stats
  from tensorflow_probability.python.internal import prefer_static
  from tensorflow_probability.python.internal import unnest
  from tensorflow_probability.python.mcmc import kernel
  from tensorflow_probability.python.mcmc.internal import util
label_desc: Use image node_id 9 for calling the DiagonalMassMatrixAdaptation obj's underlying member method code with example usage: obj.one_step(current_state, previous_kernel_results, seed) and returns: new_state,  new_kernel_results, new_variance_parts, updated_new_inner_results