Beispiel #1
0
def assert_same_structure(nest1, nest2, check_types=True):
  """Asserts that two structures are nested in the same way.

  Note that namedtuples with identical name and fields are always considered
  to have the same shallow structure (even with `check_types=True`).
  For intance, this code will print `True`:

  ```python
  def nt(a, b):
    return collections.namedtuple('foo', 'a b')(a, b)
  print(assert_same_structure(nt(0, 1), nt(2, 3)))
  ```

  Args:
    nest1: an arbitrarily nested structure.
    nest2: an arbitrarily nested structure.
    check_types: if `True` (default) types of sequences are checked as well,
        including the keys of dictionaries. If set to `False`, for example a
        list and a tuple of objects will look the same if they have the same
        size. Note that namedtuples with identical name and fields are always
        considered to have the same shallow structure. Two types will also be
        considered the same if they are both list subtypes (which allows "list"
        and "_ListWrapper" from checkpointable dependency tracking to compare
        equal).

  Raises:
    ValueError: If the two structures do not have the same number of elements or
      if the two structures are not nested in the same way.
    TypeError: If the two structures differ in the type of sequence in any of
      their substructures. Only possible if `check_types` is `True`.
  """
  _pywrap_tensorflow.AssertSameStructure(nest1, nest2, check_types)
Beispiel #2
0
def assert_same_structure(nest1,
                          nest2,
                          check_types=True,
                          expand_composites=False):
    """Asserts that two structures are nested in the same way.

  Note that namedtuples with identical name and fields are always considered
  to have the same shallow structure (even with `check_types=True`).
  For instance, this code will print `True`:

  ```python
  def nt(a, b):
    return collections.namedtuple('foo', 'a b')(a, b)
  print(assert_same_structure(nt(0, 1), nt(2, 3)))
  ```

  Args:
    nest1: an arbitrarily nested structure.
    nest2: an arbitrarily nested structure.
    check_types: if `True` (default) types of sequences are checked as well,
        including the keys of dictionaries. If set to `False`, for example a
        list and a tuple of objects will look the same if they have the same
        size. Note that namedtuples with identical name and fields are always
        considered to have the same shallow structure. Two types will also be
        considered the same if they are both list subtypes (which allows "list"
        and "_ListWrapper" from checkpointable dependency tracking to compare
        equal).
    expand_composites: If true, then composite tensors such as `tf.SparseTensor`
        and `tf.RaggedTensor` are expanded into their component tensors.

  Raises:
    ValueError: If the two structures do not have the same number of elements or
      if the two structures are not nested in the same way.
    TypeError: If the two structures differ in the type of sequence in any of
      their substructures. Only possible if `check_types` is `True`.
  """
    try:
        _pywrap_tensorflow.AssertSameStructure(nest1, nest2, check_types,
                                               expand_composites)
    except (ValueError, TypeError) as e:
        str1 = str(map_structure(lambda _: _DOT, nest1))
        str2 = str(map_structure(lambda _: _DOT, nest2))
        raise type(e)("%s\n"
                      "Entire first structure:\n%s\n"
                      "Entire second structure:\n%s" % (str(e), str1, str2))