Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/tensorflow/_api/v2/compat/v1/nest/__init__.py: 100%
7 statements
« prev ^ index » next coverage.py v7.4.0, created at 2024-01-03 07:57 +0000
« prev ^ index » next coverage.py v7.4.0, created at 2024-01-03 07:57 +0000
1# This file is MACHINE GENERATED! Do not edit.
2# Generated by: tensorflow/python/tools/api/generator/create_python_api.py script.
3"""Functions that work with structures.
5A structure is either:
7* one of the recognized Python collections, holding _nested structures_;
8* a value of any other type, typically a TensorFlow data type like Tensor,
9 Variable, or of compatible types such as int, float, ndarray, etc. these are
10 commonly referred to as _atoms_ of the structure.
12A structure of type `T` is a structure whose atomic items are of type `T`.
13For example, a structure of `tf.Tensor` only contains `tf.Tensor` as its atoms.
15Historically a _nested structure_ was called a _nested sequence_ in TensorFlow.
16A nested structure is sometimes called a _nest_ or a _tree_, but the formal
17name _nested structure_ is preferred.
19Refer to [Nesting Data Structures]
20(https://en.wikipedia.org/wiki/Nesting_(computing)#Data_structures).
22The following collection types are recognized by `tf.nest` as nested
23structures:
25* `collections.abc.Sequence` (except `string` and `bytes`).
26 This includes `list`, `tuple`, and `namedtuple`.
27* `collections.abc.Mapping` (with sortable keys).
28 This includes `dict` and `collections.OrderedDict`.
29* `collections.abc.MappingView` (with sortable keys).
30* [`attr.s` classes](https://www.attrs.org/).
32Any other values are considered **atoms**. Not all collection types are
33considered nested structures. For example, the following types are
34considered atoms:
36* `set`; `{"a", "b"}` is an atom, while `["a", "b"]` is a nested structure.
37* [`dataclass` classes](https://docs.python.org/library/dataclasses.html)
38* `tf.Tensor`
39* `numpy.array`
41`tf.nest.is_nested` checks whether an object is a nested structure or an atom.
42For example:
44 >>> tf.nest.is_nested("1234")
45 False
46 >>> tf.nest.is_nested([1, 3, [4, 5]])
47 True
48 >>> tf.nest.is_nested(((7, 8), (5, 6)))
49 True
50 >>> tf.nest.is_nested([])
51 True
52 >>> tf.nest.is_nested({"a": 1, "b": 2})
53 True
54 >>> tf.nest.is_nested({"a": 1, "b": 2}.keys())
55 True
56 >>> tf.nest.is_nested({"a": 1, "b": 2}.values())
57 True
58 >>> tf.nest.is_nested({"a": 1, "b": 2}.items())
59 True
60 >>> tf.nest.is_nested(set([1, 2]))
61 False
62 >>> ones = tf.ones([2, 3])
63 >>> tf.nest.is_nested(ones)
64 False
66Note: A proper structure shall form a tree. The user shall ensure there is no
67cyclic references within the items in the structure,
68i.e., no references in the structure of the input of these functions
69should be recursive. The behavior is undefined if there is a cycle.
72"""
74import sys as _sys
76from tensorflow.python.util.nest import assert_same_structure
77from tensorflow.python.util.nest import flatten
78from tensorflow.python.util.nest import is_nested
79from tensorflow.python.util.nest import map_structure
80from tensorflow.python.util.nest import pack_sequence_as