(self, scope=None)
| 253 | self._first_parent = weakref.ref(parent_network) |
| 254 | |
| 255 | def _set_scope(self, scope=None): |
| 256 | if self._scope is None: |
| 257 | if not self._first_parent: |
| 258 | first_parent = self._first_parent |
| 259 | else: |
| 260 | first_parent = self._first_parent() |
| 261 | if first_parent is None: |
| 262 | # If we were never added to another Network, or that Network has beed |
| 263 | # garbage collected before being called, then we're a top-level Network. |
| 264 | self._finalize_name( |
| 265 | # Use False to make sure the value sticks and we don't inherit a |
| 266 | # parent if we're added to a network later. |
| 267 | parent_network=False) |
| 268 | if scope is not None: |
| 269 | raise ValueError("Networks may not be created with explicit scopes.") |
| 270 | if first_parent: |
| 271 | first_parent._set_scope() |
| 272 | parent_scope = first_parent._scope |
| 273 | else: |
| 274 | parent_scope = self._default_parent_variable_scope |
| 275 | with variable_scope.variable_scope(parent_scope) as parent_vs: |
| 276 | expected_scope_name = parent_vs.name + "/" + self._name |
| 277 | if expected_scope_name in self._variable_scope_counts_on_init: |
| 278 | raise ValueError( |
| 279 | ("A Network named '%s' already exists (or a variable_scope was " |
| 280 | "created with this name). Names must be unique.") % |
| 281 | (self._name,)) |
| 282 | # Make sure variables with this prefix will be unique. |
| 283 | with variable_scope.variable_scope( |
| 284 | None, use_resource=True, default_name=self._name) as scope: |
| 285 | self._scope = scope |
| 286 | scope_name = scope.name |
| 287 | suffix_start = scope_name.rfind("/") + 1 |
| 288 | # rfind is -1 if there is no slash in the string, in which case the |
| 289 | # suffix starts at the beginning of the string (there is no prefix). |
| 290 | scope_suffix = scope_name[suffix_start:] |
| 291 | scope_prefix = scope_name[:suffix_start] |
| 292 | if scope_suffix != self._name: |
| 293 | raise ValueError( |
| 294 | ("A Network named '%s' already exists (or a variable_scope was " |
| 295 | "created with this name). Names must be unique.") % |
| 296 | (self._name,)) |
| 297 | if (first_parent and scope_prefix[:-1] != first_parent.scope_name): |
| 298 | raise ValueError( |
| 299 | ("Network variable names must match a nesting of sub-Network " |
| 300 | "names. Expected prefix '%s' from parent network, but got " |
| 301 | "'%s' when attempting to create a variable_scope for Network " |
| 302 | "'%s'. Likely an explicit variable_scope was inserted into " |
| 303 | "the nesting.") % |
| 304 | (first_parent.scope_name, scope_prefix[:-1], self._name)) |
| 305 | elif not first_parent and scope_prefix: |
| 306 | # For the case when this Network is not nested inside any other |
| 307 | # Network, but is in a variable_scope. This Network's name takes on |
| 308 | # the full variable scope prefix. |
| 309 | self._name = scope_name |
| 310 | |
| 311 | for non_network_sublayer in self._non_network_sublayers: |
| 312 | self._set_scope_for_nonnetwork_sublayer(non_network_sublayer) |
no test coverage detected