tf.nest.assert_same_structure  |  TensorFlow v2.16.1 (original) (raw)

tf.nest.assert_same_structure

Stay organized with collections Save and categorize content based on your preferences.

Asserts that two structures are nested in the same way.

View aliases

Compat aliases for migration

SeeMigration guide for more details.

tf.compat.v1.nest.assert_same_structure

tf.nest.assert_same_structure(
    nest1, nest2, check_types=True, expand_composites=False
)

Refer to tf.nestfor the definition of a structure.

Note the method does not check the types of atoms inside the structures.

Examples:

row_splits=[0, 4, 4, 7, 8, 8])
ragged_tensor2 = tf.RaggedTensor.from_row_splits(
values=[3, 1, 4],
row_splits=[0, 3])
tf.nest.assert_same_structure(
ragged_tensor1,
ragged_tensor2,
expand_composites=True)

collections.namedtuple('foo', 'a b')(2, 3))
Traceback (most recent call last):
``
TypeError: The two structures don't have the same nested structure

Args
nest1 an atom or a nested structure.
nest2 an atom or a nested structure.
check_types if True (default) types of structures 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 trackable dependency tracking to compare equal).check_types=True only checks type of sub-structures. The types of atoms are not checked.
expand_composites If true, then composite tensors such astf.sparse.SparseTensor and tf.RaggedTensor are expanded into their component tensors.
Raises
ValueError If the two structures do not have the same number of atoms 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.