code: def _coerce_structure(shallow_tree, input_tree):
      """Implementation of coerce_structure."""
      if not nest.is_nested(shallow_tree):
          return input_tree
  
      if not nest.is_nested(input_tree):
          raise TypeError(
              _IF_SHALLOW_IS_SEQ_INPUT_MUST_BE_SEQ.format(
                  type(input_tree)
              )
          )
  
      if len(input_tree) != len(shallow_tree):
          raise ValueError(
              _STRUCTURES_HAVE_MISMATCHING_LENGTHS.format(
                  input_length=len(input_tree),
                  shallow_length=len(shallow_tree),
              )
          )
  
      # Determine whether shallow_tree should be treated as a Mapping or a Sequence.
      # Namedtuples can be interpreted either way (but keys take precedence).
      _shallow_is_namedtuple = nest._is_namedtuple(
          shallow_tree
      )  # pylint: disable=invalid-name
      _shallow_is_mapping = isinstance(
          shallow_tree, collections.abc.Mapping
      )  # pylint: disable=invalid-name
      shallow_supports_keys = (
          _shallow_is_namedtuple or _shallow_is_mapping
      )
      shallow_supports_iter = (
          _shallow_is_namedtuple or not _shallow_is_mapping
      )
  
      # Branch-selection depends on both shallow and input container-classes.
      input_is_mapping = isinstance(input_tree, collections.abc.Mapping)
      if nest._is_namedtuple(input_tree):
          if shallow_supports_keys:
              lookup_branch = lambda k: getattr(input_tree, k)
          else:
              input_iter = nest._yield_value(input_tree)
              lookup_branch = lambda _: next(input_iter)
      elif shallow_supports_keys and input_is_mapping:
          lookup_branch = lambda k: input_tree[k]
      elif shallow_supports_iter and not input_is_mapping:
          input_iter = nest._yield_value(input_tree)
          lookup_branch = lambda _: next(input_iter)
      else:
          raise TypeError(
              _STRUCTURES_HAVE_MISMATCHING_TYPES.format(
                  input_type=type(input_tree),
                  shallow_type=(
                      type(shallow_tree.__wrapped__)
                      if hasattr(shallow_tree, "__wrapped__")
                      else type(shallow_tree)
                  ),
              )
          )
  
      flat_coerced = []
      needs_wrapping = type(shallow_tree) is not type(input_tree)
      for shallow_key, shallow_branch in nest._yield_sorted_items(
          shallow_tree
      ):
          try:
              input_branch = lookup_branch(shallow_key)
          except (KeyError, AttributeError):
              # pylint: disable=raise-missing-from
              raise ValueError(
                  _SHALLOW_TREE_HAS_INVALID_KEYS.format([shallow_key])
              )
          flat_coerced.append(
              _coerce_structure(shallow_branch, input_branch)
          )
          # Keep track of whether nested elements have changed.
          needs_wrapping |= input_branch is not flat_coerced[-1]
  
      # Only create a new instance if containers differ or contents changed.
      return (
          nest._sequence_like(shallow_tree, flat_coerced)
          if needs_wrapping
          else input_tree
      )
  
imports: import collections
  import numpy
  import tensorflow.compat.v2
  from tensorflow_probability.python.internal import prefer_static
  from tensorflow_probability.python.internal import tensor_util
  from tensorflow.python.util import nest
label_desc: Use image node_id 16 for calling a global function with example usage: _coerce_structure(shallow_tree, input_tree) and returns: unknown, input_tree