aboutsummaryrefslogtreecommitdiffhomepage
path: root/tensorflow/python/util
diff options
context:
space:
mode:
authorGravatar Eugene Brevdo <ebrevdo@google.com>2018-09-21 17:12:04 -0700
committerGravatar TensorFlower Gardener <gardener@tensorflow.org>2018-09-21 17:17:06 -0700
commit0e220a53b7c4b9ad6dd14a3bfa0ab52f6105b7cf (patch)
treeda060579beaeeca664db66225f26f0f0648ef84a /tensorflow/python/util
parent501a1955cf44568641f7205c27852eed20bc62db (diff)
[TF nest] Better error messages showing full structures on assert_same_structure
PiperOrigin-RevId: 214067946
Diffstat (limited to 'tensorflow/python/util')
-rw-r--r--tensorflow/python/util/nest.py22
-rw-r--r--tensorflow/python/util/nest_test.py6
2 files changed, 26 insertions, 2 deletions
diff --git a/tensorflow/python/util/nest.py b/tensorflow/python/util/nest.py
index 2968ca9c07..653ca525dc 100644
--- a/tensorflow/python/util/nest.py
+++ b/tensorflow/python/util/nest.py
@@ -118,6 +118,18 @@ flatten = _pywrap_tensorflow.Flatten
_same_namedtuples = _pywrap_tensorflow.SameNamedtuples
+class _DotString(object):
+
+ def __str__(self):
+ return "."
+
+ def __repr__(self):
+ return "."
+
+
+_DOT = _DotString()
+
+
def assert_same_structure(nest1, nest2, check_types=True):
"""Asserts that two structures are nested in the same way.
@@ -149,7 +161,15 @@ def assert_same_structure(nest1, nest2, check_types=True):
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)
+ try:
+ _pywrap_tensorflow.AssertSameStructure(nest1, nest2, check_types)
+ 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))
def flatten_dict_items(dictionary):
diff --git a/tensorflow/python/util/nest_test.py b/tensorflow/python/util/nest_test.py
index ef503137d1..bfb4c6f910 100644
--- a/tensorflow/python/util/nest_test.py
+++ b/tensorflow/python/util/nest_test.py
@@ -264,7 +264,11 @@ class NestTest(parameterized.TestCase, test.TestCase):
"Second structure:.*\n\n"
"More specifically: Substructure "
r'"type=tuple str=\(\(1, 2\), 3\)" is a sequence, while '
- 'substructure "type=str str=spam" is not')):
+ 'substructure "type=str str=spam" is not\n'
+ "Entire first structure:\n"
+ r"\(\(\(\., \.\), \.\), \., \(\., \.\)\)\n"
+ "Entire second structure:\n"
+ r"\(\., \.\)")):
nest.assert_same_structure(structure1, structure_different_num_elements)
with self.assertRaisesRegexp(