aboutsummaryrefslogtreecommitdiffhomepage
path: root/tensorflow/python/util
diff options
context:
space:
mode:
authorGravatar A. Unique TensorFlower <gardener@tensorflow.org>2018-07-31 09:45:26 -0700
committerGravatar TensorFlower Gardener <gardener@tensorflow.org>2018-07-31 09:49:01 -0700
commit356a9f85c0c2fadd9c11c5f201ced9fe2c490ada (patch)
treeffb6cbcf826488fe695098f4958619313180a50c /tensorflow/python/util
parent86a573b90cdbc1559e068834efc4c4b92c64202d (diff)
Add map_structure_with_paths to util.nest.
map_structure_with_paths is like map_structure, except that the string path of each leaf in the structure is passed as the first argument to the function. This can be used for example to create variables whose names are derived from the string path. PiperOrigin-RevId: 206776927
Diffstat (limited to 'tensorflow/python/util')
-rw-r--r--tensorflow/python/util/nest.py56
-rw-r--r--tensorflow/python/util/nest_test.py29
2 files changed, 85 insertions, 0 deletions
diff --git a/tensorflow/python/util/nest.py b/tensorflow/python/util/nest.py
index 5aac559b9b..faae0d89c3 100644
--- a/tensorflow/python/util/nest.py
+++ b/tensorflow/python/util/nest.py
@@ -377,6 +377,62 @@ def map_structure(func, *structure, **check_types_dict):
structure[0], [func(*x) for x in entries])
+def map_structure_with_paths(func, *structure, **kwargs):
+ """Applies `func` to each entry in `structure` and returns a new structure.
+
+ Applies `func(path, x[0], x[1], ..., **kwargs)` where x[i] is an entry in
+ `structure[i]` and `path` is the common path to x[i] in the structures. All
+ structures in `structure` must have the same arity, and the return value will
+ contain the results in the same structure. Special kwarg `check_types`
+ determines whether the types of iterables within the structure must be the
+ same-- see **kwargs definition below.
+
+ Args:
+ func: A callable with the signature func(path, *values, **kwargs) that is
+ evaluated on the leaves of the structure.
+ *structure: A variable number of compatible structures to process.
+ **kwargs: Optional kwargs to be passed through to func. Special kwarg
+ `check_types` is not passed to func, but instead determines whether the
+ types of iterables within the structures have to be same (e.g.,
+ `map_structure(func, [1], (1,))` raises a `TypeError` exception). By
+ default, the types must match. To allow iteration over structures of
+ different types (but common arity), set this kwarg to `False`.
+
+ Returns:
+ A structure of the same form as the input structures whose leaves are the
+ result of evaluating func on corresponding leaves of the input structures.
+
+ Raises:
+ TypeError: If `func` is not callable or if the structures do not match
+ each other by depth tree.
+ TypeError: If `check_types` is not `False` and the two structures differ in
+ the type of sequence in any of their substructures.
+ ValueError: If no structures are provided.
+ """
+ if not callable(func):
+ raise TypeError("func must be callable, got: %s" % func)
+ if not structure:
+ raise ValueError("Must provide at least one structure")
+
+ check_types = kwargs.pop("check_types", True)
+ for other in structure[1:]:
+ assert_same_structure(structure[0], other, check_types=check_types)
+
+ # First set paths_and_values to:
+ # [[(p11, v11), ... (p1n, v1n)], ... [(pm1, vm1), ... (pmn, vmn)]]
+ paths_and_values = [flatten_with_joined_string_paths(s) for s in structure]
+
+ # Now zip(*paths_and_values) would be:
+ # [((p11, v11), ... (pm1, vm1)), ... ((p1n, v1n), ... (pmn, vmn))]
+ # so grouped_by_path is set to:
+ # [[(p11, ... pm1), (v11, ... vm1)], ... [(p1n, ... pmn), (v1n, ... vmn)]]
+ # Note that p1i, ... pmi must all be equal since the structures are the same.
+ grouped_by_path = [zip(*p_v) for p_v in zip(*paths_and_values)]
+
+ return pack_sequence_as(structure[0], [
+ func(paths[0], *values, **kwargs) for paths, values in grouped_by_path])
+
+
def _yield_flat_up_to(shallow_tree, input_tree):
"""Yields elements `input_tree` partially flattened up to `shallow_tree`."""
if is_sequence(shallow_tree):
diff --git a/tensorflow/python/util/nest_test.py b/tensorflow/python/util/nest_test.py
index 26c6ea4b01..fd75c6885a 100644
--- a/tensorflow/python/util/nest_test.py
+++ b/tensorflow/python/util/nest_test.py
@@ -746,6 +746,35 @@ class NestTest(parameterized.TestCase, test.TestCase):
self.assertEqual(
list(nest.flatten_with_joined_string_paths(inputs)), expected)
+ @parameterized.named_parameters(
+ ("tuples", (1, 2), (3, 4), True, (("0", 4), ("1", 6))),
+ ("dicts", {"a": 1, "b": 2}, {"b": 4, "a": 3}, True,
+ {"a": ("a", 4), "b": ("b", 6)}),
+ ("mixed", (1, 2), [3, 4], False, (("0", 4), ("1", 6))),
+ ("nested",
+ {"a": [2, 3], "b": [1, 2, 3]}, {"b": [5, 6, 7], "a": [8, 9]}, True,
+ {"a": [("a/0", 10), ("a/1", 12)],
+ "b": [("b/0", 6), ("b/1", 8), ("b/2", 10)]}))
+ def testMapWithPathsCompatibleStructures(self, s1, s2, check_types, expected):
+ def format_sum(path, *values):
+ return (path, sum(values))
+ result = nest.map_structure_with_paths(format_sum, s1, s2,
+ check_types=check_types)
+ self.assertEqual(expected, result)
+
+ @parameterized.named_parameters(
+ ("tuples", (1, 2), (3, 4, 5), ValueError),
+ ("dicts", {"a": 1}, {"b": 2}, ValueError),
+ ("mixed", (1, 2), [3, 4], TypeError),
+ ("nested",
+ {"a": [2, 3], "b": [1, 3]},
+ {"b": [5, 6, 7], "a": [8, 9]},
+ ValueError
+ ))
+ def testMapWithPathsIncompatibleStructures(self, s1, s2, error_type):
+ with self.assertRaises(error_type):
+ nest.map_structure_with_paths(lambda path, *s: 0, s1, s2)
+
class NestBenchmark(test.Benchmark):