code: def constant_value_as_shape(tensor):  # pylint: disable=invalid-name
      """A version of `constant_value()` that returns a `TensorShape`.
  
      This version should be used when a constant tensor value is
      interpreted as a (possibly partial) shape, e.g. in the shape
      function for `tf.reshape()`. By explicitly requesting a
      `TensorShape` as the return value, it is possible to represent
      unknown dimensions; by contrast, `constant_value()` is
      all-or-nothing.
  
      Args:
        tensor: The rank-0 or rank-1 Tensor to be evaluated.
  
      Returns:
        A `TensorShape` based on the constant value of the given `tensor`.
  
      Raises:
        ValueError: If the shape is rank-0 and is not statically known to be -1.
      """
      shape = tf.get_static_value(tensor)
      if shape is not None:
          return tensor_shape.as_shape(
              [None if dim == -1 else dim for dim in shape]
          )
      try:
          # Importing here, conditionally, to avoid a hard dependency on
          # DeferredTensor, because that creates a BUILD dependency cycle.
          # Why is it necessary to mention DeferredTensor at all?
          # Because TF's `constant_value_as_shape` barfs on it: b/142254634.
          # NOTE: In the JAX/NumPy backends, DeferredTensor is not a class/type.
          # pylint: disable=g-import-not-at-top
          from tensorflow_probability.python.util.deferred_tensor import (
              DeferredTensor,
          )
  
          if isinstance(DeferredTensor, type) and isinstance(
              tensor, DeferredTensor
          ):
              # Presumably not constant if deferred
              return tf.TensorShape(None)
      except ImportError:
          # If DeferredTensor doesn't even exist, couldn't have been an instance of
          # it.
          pass
      if tf.executing_eagerly():
          # Working around b/142251799
          if hasattr(ops, "EagerTensor") and isinstance(
              tensor, ops.EagerTensor
          ):
              return tensor_shape.as_shape(
                  [dim if dim != -1 else None for dim in tensor.numpy()]
              )
          else:
              return tf.TensorShape(None)
      return tensor_util.constant_value_as_shape(tensor)
  
file_path: probability/tensorflow_probability/python/internal/tensorshape_util.py
imports: import numpy
  import tensorflow.compat.v2
  from tensorflow.python.framework import ops
  from tensorflow.python.framework import tensor_shape
  from tensorflow.python.framework import tensor_util
label_desc: Use image node_id 6 for calling a global function with example usage: constant_value_as_shape(tensor) and returns: tensor_util, tensor_shape, tf, tensor_shape, tf