tf.keras.tree.pack_sequence_as
Stay organized with collections
Save and categorize content based on your preferences.
Returns a given flattened sequence packed into a given structure.
tf.keras.tree.pack_sequence_as(
structure, flat_sequence, sequence_fn=None
)
If structure
is an atom, flat_sequence
must be a single-item list; in
this case the return value is flat_sequence[0]
.
If structure
is or contains a dict instance, the keys will be sorted to
pack the flat sequence in deterministic order. This is true also for
OrderedDict
instances: their sequence order is considered. The same
convention is followed in flatten
. This correctly repacks dicts and
OrderedDicts
after they have been flattened, or vice-versa.
Dictionaries with non-sortable keys cannot be flattened.
Examples:
structure = {"key3": "", "key1": "", "key2": ""}
flat_sequence = ["value1", "value2", "value3"]
keras.tree.pack_sequence_as(structure, flat_sequence)
{"key3": "value3", "key1": "value1", "key2": "value2"}
structure = (("a", "b"), ("c", "d", "e"), "f")
flat_sequence = [1.0, 2.0, 3.0, 4.0, 5.0, 6.0]
keras.tree.pack_sequence_as(structure, flat_sequence)
((1.0, 2.0), (3.0, 4.0, 5.0), 6.0)
structure = {"key3": {"c": ("alpha", "beta"), "a": ("gamma")},
"key1": {"e": "val1", "d": "val2"} }
flat_sequence = ["val2", "val1", 3.0, 1.0, 2.0]
keras.tree.pack_sequence_as(structure, flat_sequence)
{'key3': {'c': (1.0, 2.0), 'a': 3.0}, 'key1': {'e': 'val1', 'd': 'val2'} }
structure = ["a"]
flat_sequence = [np.array([[1, 2], [3, 4]])]
keras.tree.pack_sequence_as(structure, flat_sequence)
[array([[1, 2],
[3, 4]])]
structure = ["a"]
flat_sequence = [keras.ops.ones([2, 2])]
keras.tree.pack_sequence_as(structure, flat_sequence)
[array([[1., 1.],
[1., 1.]]]
Args |
structure
|
Arbitrarily nested structure.
|
flat_sequence
|
Flat sequence to pack.
|
sequence_fn
|
Defaults to _sequence_like .
|
Returns |
flat_sequence converted to have the same recursive structure as
structure .
|
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 2024-06-07 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 2024-06-07 UTC."],[],[],null,["# tf.keras.tree.pack_sequence_as\n\n\u003cbr /\u003e\n\n|---------------------------------------------------------------------------------------------------------------|\n| [View source on GitHub](https://github.com/keras-team/keras/tree/v3.3.3/keras/src/tree/tree_api.py#L208-L264) |\n\nReturns a given flattened sequence packed into a given structure. \n\n tf.keras.tree.pack_sequence_as(\n structure, flat_sequence, sequence_fn=None\n )\n\nIf `structure` is an atom, `flat_sequence` must be a single-item list; in\nthis case the return value is `flat_sequence[0]`.\n\nIf `structure` is or contains a dict instance, the keys will be sorted to\npack the flat sequence in deterministic order. This is true also for\n`OrderedDict` instances: their sequence order is considered. The same\nconvention is followed in `flatten`. This correctly repacks dicts and\n`OrderedDicts` after they have been flattened, or vice-versa.\n\nDictionaries with non-sortable keys cannot be flattened.\n\n#### Examples:\n\n structure = {\"key3\": \"\", \"key1\": \"\", \"key2\": \"\"}\n flat_sequence = [\"value1\", \"value2\", \"value3\"]\n keras.tree.pack_sequence_as(structure, flat_sequence)\n {\"key3\": \"value3\", \"key1\": \"value1\", \"key2\": \"value2\"}\n\n structure = ((\"a\", \"b\"), (\"c\", \"d\", \"e\"), \"f\")\n flat_sequence = [1.0, 2.0, 3.0, 4.0, 5.0, 6.0]\n keras.tree.pack_sequence_as(structure, flat_sequence)\n ((1.0, 2.0), (3.0, 4.0, 5.0), 6.0)\n\n structure = {\"key3\": {\"c\": (\"alpha\", \"beta\"), \"a\": (\"gamma\")},\n \"key1\": {\"e\": \"val1\", \"d\": \"val2\"} }\n flat_sequence = [\"val2\", \"val1\", 3.0, 1.0, 2.0]\n keras.tree.pack_sequence_as(structure, flat_sequence)\n {'key3': {'c': (1.0, 2.0), 'a': 3.0}, 'key1': {'e': 'val1', 'd': 'val2'} }\n\n structure = [\"a\"]\n flat_sequence = [np.array([[1, 2], [3, 4]])]\n keras.tree.pack_sequence_as(structure, flat_sequence)\n [array([[1, 2],\n [3, 4]])]\n\n structure = [\"a\"]\n flat_sequence = [keras.ops.ones([2, 2])]\n keras.tree.pack_sequence_as(structure, flat_sequence)\n [array([[1., 1.],\n [1., 1.]]]\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ---- ||\n|-----------------|-------------------------------|\n| `structure` | Arbitrarily nested structure. |\n| `flat_sequence` | Flat sequence to pack. |\n| `sequence_fn` | Defaults to `_sequence_like`. |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Returns ------- ||\n|---|---|\n| `flat_sequence` converted to have the same recursive structure as `structure`. ||\n\n\u003cbr /\u003e"]]