code: def testSampleGammaLogRateLogSpaceDerivatives(self):
      conc = tf.constant(np.linspace(0.8, 1.2, 5), tf.float64)
      rate = np.linspace(0.5, 2, 5)
      np.random.shuffle(rate)
      rate = tf.constant(rate, tf.float64)
      n = int(1e5)
  
      seed = test_util.test_seed()
      # pylint: disable=g-long-lambda
      lambdas = [  # Each should sample the same distribution.
          lambda c, r: gamma_lib.random_gamma(
              [n], c, r, seed=seed, log_space=True
          ),
          lambda c, r: gamma_lib.random_gamma(
              [n], c, log_rate=tf.math.log(r), seed=seed, log_space=True
          ),
          lambda c, r: tf.math.log(
              gamma_lib.random_gamma([n], c, r, seed=seed)
          ),
          lambda c, r: tf.math.log(
              gamma_lib.random_gamma(
                  [n], c, log_rate=tf.math.log(r), seed=seed
              )
          ),
      ]
      # pylint: enable=g-long-lambda
      samps = []
      dconc = []
      drate = []
      for fn in lambdas:
          # Take samples without the nonlinearity.
          samps.append(fn(conc, rate))
          # We compute gradient through a nonlinearity to catch a class of errors.
          _, (dc_i, dr_i) = gradient.value_and_gradient(
              lambda c, r: tf.reduce_mean(tf.square(fn(c, r))),
              (conc, rate),
          )  # pylint: disable=cell-var-from-loop
          dconc.append(dc_i)
          drate.append(dr_i)
  
      # Assert d rate correctness. Note that the non-logspace derivative for rate
      # depends on the realized sample whereas the logspace one does not. Also,
      # comparing grads with differently-placed log/exp is numerically perilous.
      self.assertAllClose(drate[0], drate[1], rtol=0.06)
      self.assertAllClose(drate[0], drate[2], rtol=0.06)
      self.assertAllClose(drate[1], drate[3], rtol=0.06)
  
      # Assert sample correctness. If incorrect, dconc will be incorrect.
      self.assertLess(
          self.evaluate(
              st.min_num_samples_for_dkwm_cdf_test(
                  discrepancy=0.04,
                  false_fail_rate=1e-9,
                  false_pass_rate=1e-9,
              )
          ),
          n,
      )
      equiv_dist = tfb.Log()(gamma_lib.Gamma(conc, rate))
      self.evaluate(
          st.assert_true_cdf_equal_by_dkwm(
              samps[0], equiv_dist.cdf, false_fail_rate=1e-9
          )
      )
      self.evaluate(
          st.assert_true_cdf_equal_by_dkwm(
              samps[1], equiv_dist.cdf, false_fail_rate=1e-9
          )
      )
      self.evaluate(
          st.assert_true_cdf_equal_by_dkwm(
              samps[2], equiv_dist.cdf, false_fail_rate=1e-9
          )
      )
      self.evaluate(
          st.assert_true_cdf_equal_by_dkwm(
              samps[3], equiv_dist.cdf, false_fail_rate=1e-9
          )
      )
  
      # Assert d concentration correctness. These are sensitive to sample values,
      # which are more strongly effected by the log/exp, thus looser tolerances.
      self.assertAllClose(dconc[0], dconc[1], rtol=0.06)
      self.assertAllClose(dconc[0], dconc[2], rtol=0.06)
      self.assertAllClose(dconc[1], dconc[3], rtol=0.06)
  
imports: import functools
  from absl.testing import parameterized
  import numpy
  from scipy import misc
  from scipy import special
  from scipy import stats
  import tensorflow.compat.v2
  from tensorflow_probability.python.bijectors import exp
  from tensorflow_probability.python.distributions import gamma
  from tensorflow_probability.python.distributions import kullback_leibler
  from tensorflow_probability.python.distributions.internal import statistical_testing
  from tensorflow_probability.python.internal import implementation_selection
  from tensorflow_probability.python.internal import test_util
  from tensorflow_probability.python.math import gradient
label_desc: Use image node_id 6 for calling the GammaSamplingTest obj's underlying member method code with example usage: obj.testSampleGammaLogRateLogSpaceDerivatives() without return types