Flattened attribute values in sorted order by attribute name. Modules are flattened by first walking their attributes in name order. Each attribute value is then flattened to find leaf values. If flatten is to be applied `recursive`ly then if the leaf is a `Module` it will also be f
(self,
recursive=True,
predicate=None,
attribute_traversal_key=None,
with_path=False)
| 193 | return tuple(self._flatten(predicate=_is_module)) |
| 194 | |
| 195 | def _flatten(self, |
| 196 | recursive=True, |
| 197 | predicate=None, |
| 198 | attribute_traversal_key=None, |
| 199 | with_path=False): |
| 200 | """Flattened attribute values in sorted order by attribute name. |
| 201 | |
| 202 | Modules are flattened by first walking their attributes in name order. |
| 203 | Each attribute value is then flattened to find leaf values. If flatten is |
| 204 | to be applied `recursive`ly then if the leaf is a `Module` it will also be |
| 205 | flattened to find leaves. Finally every leaf value is optionally tested |
| 206 | against the given `predicate` and finally yielded. |
| 207 | |
| 208 | ``` |
| 209 | class Foo(tf.Module): |
| 210 | def __init__(self): |
| 211 | super(Foo, self).__init__() |
| 212 | self.x = [tf.constant('a'), tf.constant('b')] |
| 213 | self.y = {'i': tf.constant('c'), 'j': tf.constant('d')} |
| 214 | self.z = tf.constant('e') |
| 215 | |
| 216 | @property |
| 217 | def tensors(self): |
| 218 | return tuple(self._flatten(predicate=is_tensor, with_path=True)) |
| 219 | |
| 220 | foo = Foo() |
| 221 | foo.tensors |
| 222 | # ==> ((('x', 0), <tf.Tensor: ...'a'>), |
| 223 | # (('x', 1), <tf.Tensor: ...'b'>), |
| 224 | # (('y', 'i'), <tf.Tensor: ...'c'>), |
| 225 | # (('y', 'j'), <tf.Tensor: ...'d'>), |
| 226 | # (('z',), <tf.Tensor: ...'e'>)) |
| 227 | ``` |
| 228 | |
| 229 | `attribute_traversal_key` controls the order object properties are visited. |
| 230 | If not set objects are visited in ascending order by name. |
| 231 | |
| 232 | Args: |
| 233 | recursive: Whether to recurse into child modules or not. |
| 234 | predicate: (Optional) If set then only values matching predicate are |
| 235 | yielded. A value of `None` (the default) means no items will be |
| 236 | filtered. |
| 237 | attribute_traversal_key: (Optional) Method to rekey object attributes |
| 238 | before they are sorted. Contract is the same as `key` argument to |
| 239 | builtin `sorted` and only applies to object properties. |
| 240 | with_path: (Optional) Whether to include the path to the object as well |
| 241 | as the object itself. If `with_path` is `True` then leaves will not be |
| 242 | de-duplicated (e.g. if the same leaf instance is reachable via multiple |
| 243 | modules then it will be yielded multiple times with different paths). |
| 244 | |
| 245 | Returns: |
| 246 | Flat generator for leaves of the current module and optionally all |
| 247 | submodules. |
| 248 | """ |
| 249 | if predicate is None: |
| 250 | predicate = lambda _: True |
| 251 | |
| 252 | return _flatten_module( |