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.
tf.nest.assert_same_structure(
nest1, nest2, check_types=True, expand_composites=False
)
Note the method does not check the types of data inside the structures.
Examples:
These scalar vs. scalar comparisons will pass:
tf.nest.assert_same_structure(1.5, tf.Variable(1, tf.uint32))
tf.nest.assert_same_structure("abc", np.array([1, 2]))
These sequence vs. sequence comparisons will pass:
structure1 = (((1, 2), 3), 4, (5, 6))
structure2 = ((("foo1", "foo2"), "foo3"), "foo4", ("foo5", "foo6"))
structure3 = [(("a", "b"), "c"), "d", ["e", "f"]]
tf.nest.assert_same_structure(structure1, structure2)
tf.nest.assert_same_structure(structure1, structure3, check_types=False)
import collections
tf.nest.assert_same_structure(
collections.namedtuple("bar", "a b")(1, 2),
collections.namedtuple("foo", "a b")(2, 3),
check_types=False)
tf.nest.assert_same_structure(
collections.namedtuple("bar", "a b")(1, 2),
{ "a": 1, "b": 2 },
check_types=False)
tf.nest.assert_same_structure(
{ "a": 1, "b": 2, "c": 3 },
{ "c": 6, "b": 5, "a": 4 })
ragged_tensor1 = tf.RaggedTensor.from_row_splits(
values=[3, 1, 4, 1, 5, 9, 2, 6],
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)
These examples will raise exceptions:
tf.nest.assert_same_structure([0, 1], np.array([0, 1]))
Traceback (most recent call last):
ValueError: The two structures don't have the same nested structure
tf.nest.assert_same_structure(
collections.namedtuple('bar', 'a b')(1, 2),
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 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 trackable dependency tracking to compare
equal).
|
expand_composites
|
If true, then composite tensors such as
tf.sparse.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 .
|
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates. Some content is licensed under the numpy license.
Last updated 2022-11-04 UTC.
[null,null,["Last updated 2022-11-04 UTC."],[],[],null,["# tf.nest.assert_same_structure\n\n\u003cbr /\u003e\n\n|------------------------------------------------------------------------------------------------------------------------|\n| [View source on GitHub](https://github.com/tensorflow/tensorflow/blob/v2.7.4/tensorflow/python/util/nest.py#L439-L535) |\n\nAsserts that two structures are nested in the same way.\n\n#### View aliases\n\n\n**Compat aliases for migration**\n\nSee\n[Migration guide](https://www.tensorflow.org/guide/migrate) for\nmore details.\n\n[`tf.compat.v1.nest.assert_same_structure`](https://www.tensorflow.org/api_docs/python/tf/nest/assert_same_structure)\n\n\u003cbr /\u003e\n\n tf.nest.assert_same_structure(\n nest1, nest2, check_types=True, expand_composites=False\n )\n\nNote the method does not check the types of data inside the structures.\n\n#### Examples:\n\n- These scalar vs. scalar comparisons will pass:\n\n tf.nest.assert_same_structure(1.5, tf.Variable(1, tf.uint32))\n tf.nest.assert_same_structure(\"abc\", np.array([1, 2]))\n \n- These sequence vs. sequence comparisons will pass:\n\n structure1 = (((1, 2), 3), 4, (5, 6))\n structure2 = (((\"foo1\", \"foo2\"), \"foo3\"), \"foo4\", (\"foo5\", \"foo6\"))\n structure3 = [((\"a\", \"b\"), \"c\"), \"d\", [\"e\", \"f\"]]\n tf.nest.assert_same_structure(structure1, structure2)\n tf.nest.assert_same_structure(structure1, structure3, check_types=False)\n \n import collections\n tf.nest.assert_same_structure(\n collections.namedtuple(\"bar\", \"a b\")(1, 2),\n collections.namedtuple(\"foo\", \"a b\")(2, 3),\n check_types=False)\n \n tf.nest.assert_same_structure(\n collections.namedtuple(\"bar\", \"a b\")(1, 2),\n { \"a\": 1, \"b\": 2 },\n check_types=False)\n \n tf.nest.assert_same_structure(\n { \"a\": 1, \"b\": 2, \"c\": 3 },\n { \"c\": 6, \"b\": 5, \"a\": 4 })\n \n ragged_tensor1 = tf.RaggedTensor.from_row_splits(\n values=[3, 1, 4, 1, 5, 9, 2, 6],\n row_splits=[0, 4, 4, 7, 8, 8])\n ragged_tensor2 = tf.RaggedTensor.from_row_splits(\n values=[3, 1, 4],\n row_splits=[0, 3])\n tf.nest.assert_same_structure(\n ragged_tensor1,\n ragged_tensor2,\n expand_composites=True)\n \n- These examples will raise exceptions:\n\n tf.nest.assert_same_structure([0, 1], np.array([0, 1]))\n Traceback (most recent call last):\n\n ValueError: The two structures don't have the same nested structure\n \n tf.nest.assert_same_structure(\n collections.namedtuple('bar', 'a b')(1, 2),\n collections.namedtuple('foo', 'a b')(2, 3))\n Traceback (most recent call last):\n\n TypeError: The two structures don't have the same nested structure\n \n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ---- ||\n|---------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| `nest1` | an arbitrarily nested structure. |\n| `nest2` | an arbitrarily nested structure. |\n| `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 trackable dependency tracking to compare equal). |\n| `expand_composites` | If true, then composite tensors such as [`tf.sparse.SparseTensor`](../../tf/sparse/SparseTensor) and [`tf.RaggedTensor`](../../tf/RaggedTensor) are expanded into their component tensors. |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Raises ------ ||\n|--------------|-------------------------------------------------------------------------------------------------------------------------------|\n| `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. |\n| `TypeError` | If the two structures differ in the type of sequence in any of their substructures. Only possible if `check_types` is `True`. |\n\n\u003cbr /\u003e"]]