Track a Layer in this Network. `Network` requires that all `Layer`s used in `call()` be tracked so that the `Network` can export a complete list of variables. Args: layer: A `tf.compat.v1.layers.Layer` object. Returns: The passed in `layer`. Raises: RuntimeE
(self, layer)
| 348 | return self._name |
| 349 | |
| 350 | def track_layer(self, layer): |
| 351 | """Track a Layer in this Network. |
| 352 | |
| 353 | `Network` requires that all `Layer`s used in `call()` be tracked so that the |
| 354 | `Network` can export a complete list of variables. |
| 355 | |
| 356 | Args: |
| 357 | layer: A `tf.compat.v1.layers.Layer` object. |
| 358 | |
| 359 | Returns: |
| 360 | The passed in `layer`. |
| 361 | |
| 362 | Raises: |
| 363 | RuntimeError: If __init__ has not been called. |
| 364 | TypeError: If `layer` is the wrong type. |
| 365 | ValueError: If a `Layer` with the same name has already been added. |
| 366 | """ |
| 367 | if not hasattr(self, "_layers"): |
| 368 | raise RuntimeError("Need to call Network.__init__ before adding layers") |
| 369 | if not isinstance(layer, base.Layer): |
| 370 | raise TypeError( |
| 371 | "Network.track_layer() passed type %s, not a tf.layers.Layer" % |
| 372 | (type(layer),)) |
| 373 | # Always use `ResourceVariable` with legacy layers. |
| 374 | layer._use_resource_variables = True |
| 375 | if isinstance(layer, Network): |
| 376 | layer._finalize_name(parent_network=self) |
| 377 | else: |
| 378 | # `layer` is a non-Network, so it hasn't been named to follow Network |
| 379 | # conventions for contained Layers (i.e. the same conventions as for |
| 380 | # sub-Networks). This renaming is necessary to isolate Network variable |
| 381 | # naming from Layers constructed outside the Network and never added to it |
| 382 | # (because Layers are named globally). |
| 383 | if not layer.built: |
| 384 | if not hasattr(layer, "_first_parent"): |
| 385 | dereferenced_layer_first_parent = None |
| 386 | else: |
| 387 | dereferenced_layer_first_parent = layer._first_parent() |
| 388 | if dereferenced_layer_first_parent is None: |
| 389 | if layer._name != layer._base_name: |
| 390 | # If name and base_name do not match, then this Layer used anonymous |
| 391 | # naming and we have to rename it. Otherwise there's an explicit |
| 392 | # name, and we should respect it (subject to error checking). |
| 393 | layer._name, layer._base_name = layer._make_unique_name( |
| 394 | name_uid_map=self._sub_layer_name_uids, |
| 395 | avoid_names=self._owned_layers, |
| 396 | zero_based=True |
| 397 | # No namespace required, since we've specified our own UID map. |
| 398 | ) |
| 399 | layer._first_parent = weakref.ref(self) |
| 400 | self._non_network_sublayers.append(layer) |
| 401 | if (not layer.built and layer._first_parent and |
| 402 | self is layer._first_parent()): |
| 403 | if layer.name in self._owned_layers: |
| 404 | if self._owned_layers[layer.name] is layer: |
| 405 | return layer |
| 406 | raise ValueError( |
| 407 | "Attempt to add two Layers with the name '%s' to the same Network." |