class_docstr: Replica Exchange MCMC [@swendsen1986replica; @hukushima1996exchange].
  
  #### Examples
  ```python
  cat = Categorical(probs=[0.5,0.5])
  x = Mixture(cat=cat, components=[
      MultivariateNormalDiag([0.0,0.0], [1.0,1.0]),
      MultivariateNormalDiag([10.0,10.0], [1.0,1.0])])
  proposal_x = MultivariateNormalDiag(x, [1.0,1.0])
  qx = Empirical(tf.Variable(tf.zeros([10000, 2])))
  inference = ed.ReplicaExchangeMC(latent_vars={x: qx},
                                   proposal_vars={x: proposal_x})
  ```
class_docstr_tok: ["Replica","Exchange","MCMC","[","@","swendsen1986replica",";","@","hukushima1996exchange","]",".","#","#","#","#","Examples","``","`","python","cat","=","Categorical","(","probs=","[","0.5,0.5","]",")","x","=","Mixture","(","cat=cat",",","components=","[","MultivariateNormalDiag","(","[","0.0,0.0","]",",","[","1.0,1.0","]",")",",","MultivariateNormalDiag","(","[","10.0,10.0","]",",","[","1.0,1.0","]",")","]",")","proposal_x","=","MultivariateNormalDiag","(","x",",","[","1.0,1.0","]",")","qx","=","Empirical","(","tf.Variable","(","tf.zeros","(","[","10000",",","2","]",")",")",")","inference","=","ed.ReplicaExchangeMC","(","latent_vars=","{","x",":","qx","}",",","proposal_vars=","{","x",":","proposal_x","}",")","``","`"]
docstr: Draw sample by Metropolis-Hastings. Then
  accept or reject the sample based on the ratio,
  $\text{ratio} = \text{inverse_temperature}(
        \log p(x, z^{\text{new}}) - \log p(x, z^{\text{old}}) -
        \log g(z^{\text{new}} \mid z^{\text{old}}) +
        \log g(z^{\text{old}} \mid z^{\text{new}}))$
docstr_tok: ["Draw","sample","by","Metropolis-Hastings",".","Then","accept","or","reject","the","sample","based","on","the","ratio",",","$","\\text","{","ratio","}","=","\\text","{","inverse_temperature","}","(","\\log","p","(","x",",","z^","{","\\text","{","new","}","}",")","-","\\log","p","(","x",",","z^","{","\\text","{","old","}","}",")","-","\\log","g","(","z^","{","\\text","{","new","}","}","\\mid","z^","{","\\text","{","old","}","}",")","+","\\log","g","(","z^","{","\\text","{","old","}","}","\\mid","z^","{","\\text","{","new","}","}",")",")","$"]
code: def _mh_sample(self, latent_vars, inverse_temperature):
      """Draw sample by Metropolis-Hastings. Then
      accept or reject the sample based on the ratio,
      $\\text{ratio} = \\text{inverse_temperature}(
            \log p(x, z^{\\text{new}}) - \log p(x, z^{\\text{old}}) -
            \log g(z^{\\text{new}} \mid z^{\\text{old}}) +
            \log g(z^{\\text{old}} \mid z^{\\text{new}}))$
      """
      old_sample = {
          z: tf.gather(qz.params, tf.maximum(self.t - 1, 0))
          for z, qz in six.iteritems(latent_vars)
      }
      old_sample = OrderedDict(old_sample)
  
      # Form dictionary in order to replace conditioning on prior or
      # observed variable with conditioning on a specific value.
      dict_swap = {}
      for x, qx in six.iteritems(self.data):
          if isinstance(x, RandomVariable):
              if isinstance(qx, RandomVariable):
                  qx_copy = copy(qx, scope="conditional")
                  dict_swap[x] = qx_copy.value()
              else:
                  dict_swap[x] = qx
      dict_swap_old = dict_swap.copy()
      dict_swap_old.update(old_sample)
      base_scope = tf.get_default_graph().unique_name("inference") + "/"
      scope_old = base_scope + "old"
      scope_new = base_scope + "new"
  
      # Draw proposed sample and calculate acceptance ratio.
      new_sample = old_sample.copy()  # copy to ensure same order
      ratio = 0.0
      for z, proposal_z in six.iteritems(self.proposal_vars):
          # Build proposal g(znew | zold).
          proposal_znew = copy(
              proposal_z, dict_swap_old, scope=scope_old
          )
          # Sample znew ~ g(znew | zold).
          new_sample[z] = proposal_znew.value()
          # Increment ratio.
          ratio -= tf.reduce_sum(proposal_znew.log_prob(new_sample[z]))
  
      dict_swap_new = dict_swap.copy()
      dict_swap_new.update(new_sample)
  
      for z, proposal_z in six.iteritems(self.proposal_vars):
          # Build proposal g(zold | znew).
          proposal_zold = copy(
              proposal_z, dict_swap_new, scope=scope_new
          )
          # Increment ratio.
          ratio += tf.reduce_sum(
              proposal_zold.log_prob(dict_swap_old[z])
          )
  
      for z in six.iterkeys(latent_vars):
          # Build priors p(znew) and p(zold).
          znew = copy(z, dict_swap_new, scope=scope_new)
          zold = copy(z, dict_swap_old, scope=scope_old)
          # Increment ratio.
          ratio += tf.reduce_sum(znew.log_prob(dict_swap_new[z]))
          ratio -= tf.reduce_sum(zold.log_prob(dict_swap_old[z]))
  
      for x in six.iterkeys(self.data):
          if isinstance(x, RandomVariable):
              # Build likelihoods p(x | znew) and p(x | zold).
              x_znew = copy(x, dict_swap_new, scope=scope_new)
              x_zold = copy(x, dict_swap_old, scope=scope_old)
              # Increment ratio.
              ratio += tf.reduce_sum(x_znew.log_prob(dict_swap[x]))
              ratio -= tf.reduce_sum(x_zold.log_prob(dict_swap[x]))
  
      ratio *= inverse_temperature
  
      # Accept or reject sample.
      u = tf.random_uniform([], dtype=ratio.dtype)
      accept = tf.log(u) < ratio
      sample_values = tf.cond(
          accept,
          lambda: list(six.itervalues(new_sample)),
          lambda: list(six.itervalues(old_sample)),
      )
      if not isinstance(sample_values, list):
          # `tf.cond` returns tf.Tensor if output is a list of size 1.
          sample_values = [sample_values]
  
      sample = {
          z: sample_value
          for z, sample_value in zip(
              six.iterkeys(new_sample), sample_values
          )
      }
      return sample, accept
  
imports: from __future__ import absolute_import
  from __future__ import division
  from __future__ import print_function
  import six
  import tensorflow
  import numpy
  from collections import OrderedDict
  from edward.inferences.monte_carlo import MonteCarlo
  from edward.models import Empirical, RandomVariable
  from edward.util import check_latent_vars, copy
label_desc: Use image node_id 4 for calling the ReplicaExchangeMC obj's underlying member method code with example usage: obj._mh_sample(latent_vars, inverse_temperature) and returns: sample,  accept