code: def _distributional_to_value(value_d, size, subscale, threshold):
      """Get a scalar value out of a value distribution in distributional RL."""
      half = size // 2
      value_range = (
          tf.to_float(tf.range(-half, half)) + 0.5
      ) * subscale
      probs = tf.nn.softmax(value_d)
  
      if threshold == 0.0:
          return tf.reduce_sum(probs * value_range, axis=-1)
  
      # accumulated_probs[..., i] is the sum of probabilities in buckets upto i
      # so it is the probability that value <= i'th bucket value
      accumulated_probs = tf.cumsum(probs, axis=-1)
      # New probs are 0 on all lower buckets, until the threshold
      probs = tf.where(
          accumulated_probs < threshold, tf.zeros_like(probs), probs
      )
      probs /= tf.reduce_sum(
          probs, axis=-1, keepdims=True
      )  # Re-normalize.
      return tf.reduce_sum(probs * value_range, axis=-1)
  
file_path: tensor2tensor/tensor2tensor/rl/ppo.py
imports: from __future__ import absolute_import
  from __future__ import division
  from __future__ import print_function
  from tensor2tensor.layers import common_layers
  from tensor2tensor.models.research.rl import get_policy
  from tensor2tensor.utils import learning_rate
  from tensor2tensor.utils import optimize
  import tensorflow.compat.v1
  import tensorflow_probability
label_desc: Use image node_id 2 for calling a global function with example usage: _distributional_to_value(value_d, size, subscale, threshold) and returns: tf, tf