Helper function for sharing scopes. All the parameters within the shared_scopes, will be remapped with the respect of CurrentNamescope() I.e. if one calls ParameterSharing with {'scope_b': 'scope_'a'}, from the scope 'some_global_scope', it'll effectively mean, that all paramet
(shared_scopes)
| 86 | |
| 87 | @contextlib.contextmanager |
| 88 | def ParameterSharing(shared_scopes): |
| 89 | """ |
| 90 | Helper function for sharing scopes. |
| 91 | All the parameters within the shared_scopes, will be remapped with the |
| 92 | respect of CurrentNamescope() |
| 93 | |
| 94 | I.e. if one calls ParameterSharing with {'scope_b': 'scope_'a'}, from the |
| 95 | scope 'some_global_scope', it'll effectively mean, that all parameters from |
| 96 | 'some_global_scope/scope_b' will shared with the parameters from |
| 97 | 'some_global_scope/scope_a' |
| 98 | """ |
| 99 | assert isinstance(shared_scopes, dict) |
| 100 | |
| 101 | shared_scope_overrides = {} |
| 102 | current_scope = scope.CurrentNameScope() |
| 103 | for k, v in shared_scopes.items(): |
| 104 | assert not v.startswith(k), ( |
| 105 | "Illegal override for parameter sharing. {} is prefix of {}". |
| 106 | format(k, v)) |
| 107 | k = current_scope + k |
| 108 | v = current_scope + v |
| 109 | # Normalize all the scopes, so scope_a and scope_a/ are equivalent |
| 110 | k = _normalize_namescope(k) |
| 111 | v = _normalize_namescope(v) |
| 112 | shared_scope_overrides[k] = v |
| 113 | |
| 114 | try: |
| 115 | parameter_sharing_context.add_scope_overrides(shared_scope_overrides) |
| 116 | yield |
| 117 | finally: |
| 118 | parameter_sharing_context.pop() |
searching dependent graphs…