def shape(tensor, dim=None):
  """Gets the most specific dimension size(s) of the given tensor.

  This is a wrapper around the two ways to get the shape of a tensor: (1)
  t.get_shape() to get the static shape, and (2) tf.shape(t) to get the dynamic
  shape. This function returns the most specific available size for each
  dimension. If the static size is available, that is returned. Otherwise, the
  tensor representing the dynamic size is returned.

  Args:
    tensor: Input tensor.
    dim: Desired dimension. Use None to retrieve the list of all sizes.

  Returns:
    output = Most specific static dimension size(s).
  """
  static_shape = tensor.get_shape()
  dynamic_shape = tf.shape(tensor)
  if dim is not None:
    return static_shape[dim].value or dynamic_shape[dim]
  else:
    return [d.value or dynamic_shape[i] for i, d in enumerate(static_shape)]
Gets the most specific dimension size(s) of the given tensor.

  This is a wrapper around the two ways to get the shape of a tensor: (1)
  t.get_shape() to get the static shape, and (2) tf.shape(t) to get the dynamic
  shape. This function returns the most specific available size for each
  dimension. If the static size is available, that is returned. Otherwise, the
  tensor representing the dynamic size is returned.

  Args:
    tensor: Input tensor.
    dim: Desired dimension. Use None to retrieve the list of all sizes.

  Returns:
    output = Most specific static dimension size(s).
  
language/labs/exemplar_decoding/utils/tensor_utils.py