tf.contrib.framework.nest.map_structure_up_to
Stay organized with collections
Save and categorize content based on your preferences.
Applies a function or op to a number of partially flattened inputs.
tf.contrib.framework.nest.map_structure_up_to(
shallow_tree, func, *inputs, **kwargs
)
The inputs
are flattened up to shallow_tree
before being mapped.
Use Case:
Sometimes we wish to apply a function to a partially flattened
sequence (for example when the function itself takes sequence inputs). We
achieve this by specifying a shallow structure, shallow_tree
we wish to
flatten up to.
The inputs
, can be thought of as having the same structure layout as
shallow_tree
, but with leaf nodes that are themselves tree structures.
This function therefore will return something with the same base structure as
shallow_tree
.
Examples:
shallow_tree = [None, None]
inp_val = [1, 2, 3]
out = map_structure_up_to(shallow_tree, lambda x: 2 * x, inp_val)
# Output is: [2, 4]
ab_tuple = collections.namedtuple("ab_tuple", "a, b")
op_tuple = collections.namedtuple("op_tuple", "add, mul")
inp_val = ab_tuple(a=2, b=3)
inp_ops = ab_tuple(a=op_tuple(add=1, mul=2), b=op_tuple(add=2, mul=3))
out = map_structure_up_to(inp_val, lambda val, ops: (val + ops.add) * ops.mul,
inp_val, inp_ops)
# Output is: ab_tuple(a=6, b=15)
data_list = [[2, 4, 6, 8], [[1, 3, 5, 7, 9], [3, 5, 7]]]
name_list = ['evens', ['odds', 'primes']]
out = map_structure_up_to(
name_list,
lambda name, sec: "first_{}_{}".format(len(sec), name),
name_list, data_list)
# Output is: ['first_4_evens', ['first_5_odds', 'first_3_primes']]
Args |
shallow_tree
|
a shallow tree, common to all the inputs.
|
func
|
callable which will be applied to each input individually.
|
*inputs
|
arbitrarily nested combination of objects that are compatible with
shallow_tree. The function func is applied to corresponding
partially flattened elements of each input, so the function must support
arity of len(inputs) .
|
**kwargs
|
kwargs to feed 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). To allow
this set this argument to False .
|
Raises |
TypeError
|
If shallow_tree is a sequence but input_tree is not.
|
TypeError
|
If the sequence types of shallow_tree are different from
input_tree .
|
ValueError
|
If the sequence lengths of shallow_tree are different from
input_tree .
|
Returns |
result of repeatedly applying func , with the same structure layout as
shallow_tree .
|
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.
Last updated 2020-10-01 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2020-10-01 UTC."],[],[],null,["# tf.contrib.framework.nest.map_structure_up_to\n\n\u003cbr /\u003e\n\n|--------------------------------------------------------------------------------------------------------------------------|\n| [View source on GitHub](https://github.com/tensorflow/tensorflow/blob/v1.15.0/tensorflow/python/util/nest.py#L991-L1069) |\n\nApplies a function or op to a number of partially flattened inputs. \n\n tf.contrib.framework.nest.map_structure_up_to(\n shallow_tree, func, *inputs, **kwargs\n )\n\nThe `inputs` are flattened up to `shallow_tree` before being mapped.\n\n#### Use Case:\n\nSometimes we wish to apply a function to a partially flattened\nsequence (for example when the function itself takes sequence inputs). We\nachieve this by specifying a shallow structure, `shallow_tree` we wish to\nflatten up to.\n\nThe `inputs`, can be thought of as having the same structure layout as\n`shallow_tree`, but with leaf nodes that are themselves tree structures.\n\nThis function therefore will return something with the same base structure as\n`shallow_tree`.\n\n#### Examples:\n\n shallow_tree = [None, None]\n inp_val = [1, 2, 3]\n out = map_structure_up_to(shallow_tree, lambda x: 2 * x, inp_val)\n\n # Output is: [2, 4]\n\n ab_tuple = collections.namedtuple(\"ab_tuple\", \"a, b\")\n op_tuple = collections.namedtuple(\"op_tuple\", \"add, mul\")\n inp_val = ab_tuple(a=2, b=3)\n inp_ops = ab_tuple(a=op_tuple(add=1, mul=2), b=op_tuple(add=2, mul=3))\n out = map_structure_up_to(inp_val, lambda val, ops: (val + ops.add) * ops.mul,\n inp_val, inp_ops)\n\n # Output is: ab_tuple(a=6, b=15)\n\n data_list = [[2, 4, 6, 8], [[1, 3, 5, 7, 9], [3, 5, 7]]]\n name_list = ['evens', ['odds', 'primes']]\n out = map_structure_up_to(\n name_list,\n lambda name, sec: \"first_{}_{}\".format(len(sec), name),\n name_list, data_list)\n\n # Output is: ['first_4_evens', ['first_5_odds', 'first_3_primes']]\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ---- ||\n|----------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| `shallow_tree` | a shallow tree, common to all the inputs. |\n| `func` | callable which will be applied to each input individually. |\n| `*inputs` | arbitrarily nested combination of objects that are compatible with shallow_tree. The function `func` is applied to corresponding partially flattened elements of each input, so the function must support arity of `len(inputs)`. |\n| `**kwargs` | kwargs to feed 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). To allow this set this argument to `False`. |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Raises ------ ||\n|--------------|----------------------------------------------------------------------------|\n| `TypeError` | If `shallow_tree` is a sequence but `input_tree` is not. |\n| `TypeError` | If the sequence types of `shallow_tree` are different from `input_tree`. |\n| `ValueError` | If the sequence lengths of `shallow_tree` are different from `input_tree`. |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Returns ------- ||\n|---|---|\n| result of repeatedly applying `func`, with the same structure layout as `shallow_tree`. ||\n\n\u003cbr /\u003e"]]