tf.nest.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.nest.pack_sequence_as(
structure, flat_sequence, expand_composites=False
)
If structure
is a scalar, flat_sequence
must be a single-element 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 ignored, the sorting order of
keys is used instead. The same convention is followed in flatten
.
This correctly repacks dicts and OrderedDict
s after they have been
flattened, and also allows flattening an OrderedDict
and then repacking it
back using a corresponding plain dict, or vice-versa.
Dictionaries with non-sortable keys cannot be flattened.
Args |
structure
|
Nested structure, whose structure is given by nested lists,
tuples, and dicts. Note: numpy arrays and strings are considered
scalars.
|
flat_sequence
|
flat sequence to pack.
|
expand_composites
|
If true, then composite tensors such as tf.SparseTensor
and tf.RaggedTensor are expanded into their component tensors.
|
Returns |
packed
|
flat_sequence converted to have the same recursive structure as
structure .
|
Raises |
ValueError
|
If flat_sequence and structure have different
element counts.
|
TypeError
|
structure is or contains a dict with non-sortable keys.
|
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.
[null,null,["Last updated 2020-10-01 UTC."],[],[],null,["# tf.nest.pack_sequence_as\n\n\u003cbr /\u003e\n\n|----------------------------------------------------------------------------------|------------------------------------------------------------------------------------------------------------------------|\n| [TensorFlow 1 version](/versions/r1.15/api_docs/python/tf/nest/pack_sequence_as) | [View source on GitHub](https://github.com/tensorflow/tensorflow/blob/v2.1.0/tensorflow/python/util/nest.py#L471-L504) |\n\nReturns a given flattened sequence packed into a given structure.\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.pack_sequence_as`](/api_docs/python/tf/nest/pack_sequence_as)\n\n\u003cbr /\u003e\n\n tf.nest.pack_sequence_as(\n structure, flat_sequence, expand_composites=False\n )\n\nIf `structure` is a scalar, `flat_sequence` must be a single-element list;\nin this 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 ignored, the sorting order of\nkeys is used instead. The same convention is followed in `flatten`.\nThis correctly repacks dicts and `OrderedDict`s after they have been\nflattened, and also allows flattening an `OrderedDict` and then repacking it\nback using a corresponding plain dict, or vice-versa.\nDictionaries with non-sortable keys cannot be flattened.\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ---- ||\n|---------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| `structure` | Nested structure, whose structure is given by nested lists, tuples, and dicts. Note: numpy arrays and strings are considered scalars. |\n| `flat_sequence` | flat sequence to pack. |\n| `expand_composites` | If true, then composite tensors such as [`tf.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| Returns ------- ||\n|----------|--------------------------------------------------------------------------------|\n| `packed` | `flat_sequence` converted to have the same recursive structure as `structure`. |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Raises ------ ||\n|--------------|-------------------------------------------------------------------|\n| `ValueError` | If `flat_sequence` and `structure` have different element counts. |\n| `TypeError` | `structure` is or contains a dict with non-sortable keys. |\n\n\u003cbr /\u003e"]]