class_docstr: Tests for discrete_policy_entropy op.
class_docstr_tok: ["Tests","for","discrete_policy_entropy","op","."]
code: def testGradient(self, is_multi_actions):
      with self.test_session() as sess:
          policy_logits_np = np.array(
              [[0, 1], [1, 2], [0, 2], [1, 1], [0, -1000], [0, 1000]]
          )
          if is_multi_actions:
              num_action_components = 3
              policy_logits_nest = [
                  tf.constant(policy_logits_np, dtype=tf.float32)
                  for _ in xrange(num_action_components)
              ]
          else:
              num_action_components = 1
              policy_logits_nest = tf.constant(
                  policy_logits_np, dtype=tf.float32
              )
  
          entropy_op = pg_ops.discrete_policy_entropy_loss(
              policy_logits_nest
          )
          entropy = entropy_op.extra.entropy
          # Counterintuitively, the gradient->0 as policy->deterministic, that's why
          # the gradients for the large logit cases are `[0, 0]`. They should
          # strictly be >0, but they get truncated when we run out of precision.
          expected_gradients = np.array(
              [
                  [0.1966119, -0.1966119],
                  [0.1966119, -0.1966119],
                  [0.2099872, -0.2099872],
                  [0, 0],
                  [0, 0],
                  [0, 0],
              ]
          )
          for policy_logits in nest.flatten(policy_logits_nest):
              gradients = tf.gradients(entropy, policy_logits)
              grad_policy_logits = sess.run(gradients[0])
              self.assertAllClose(
                  grad_policy_logits, expected_gradients, atol=1e-4
              )
  
imports: from __future__ import absolute_import
  from __future__ import division
  from __future__ import print_function
  from absl.testing import parameterized
  import numpy
  from six.moves import xrange
  import tensorflow.compat.v1
  import tree
  from trfl import discrete_policy_gradient_ops
label_desc: Use image node_id 2 for calling the EntropyCostTest obj's underlying member method code with example usage: obj.testGradient(is_multi_actions) without return types