Stores the default arguments for the given set of list_ops. For usage, please see examples at top of the file. Args: list_ops_or_scope: List or tuple of operations to set argument scope for or a dictionary containing the current scope. When list_ops_or_scope is a dict, kwargs m
(list_ops_or_scope, **kwargs)
| 109 | |
| 110 | @tf_contextlib.contextmanager |
| 111 | def arg_scope(list_ops_or_scope, **kwargs): |
| 112 | """Stores the default arguments for the given set of list_ops. |
| 113 | |
| 114 | For usage, please see examples at top of the file. |
| 115 | |
| 116 | Args: |
| 117 | list_ops_or_scope: List or tuple of operations to set argument scope for or |
| 118 | a dictionary containing the current scope. When list_ops_or_scope is a |
| 119 | dict, kwargs must be empty. When list_ops_or_scope is a list or tuple, |
| 120 | then every op in it need to be decorated with @add_arg_scope to work. |
| 121 | **kwargs: keyword=value that will define the defaults for each op in |
| 122 | list_ops. All the ops need to accept the given set of arguments. |
| 123 | |
| 124 | Yields: |
| 125 | the current_scope, which is a dictionary of {op: {arg: value}} |
| 126 | Raises: |
| 127 | TypeError: if list_ops is not a list or a tuple. |
| 128 | ValueError: if any op in list_ops has not be decorated with @add_arg_scope. |
| 129 | """ |
| 130 | if isinstance(list_ops_or_scope, dict): |
| 131 | # Assumes that list_ops_or_scope is a scope that is being reused. |
| 132 | if kwargs: |
| 133 | raise ValueError('When attempting to re-use a scope by suppling a' |
| 134 | 'dictionary, kwargs must be empty.') |
| 135 | current_scope = list_ops_or_scope.copy() |
| 136 | try: |
| 137 | _get_arg_stack().append(current_scope) |
| 138 | yield current_scope |
| 139 | finally: |
| 140 | _get_arg_stack().pop() |
| 141 | else: |
| 142 | # Assumes that list_ops_or_scope is a list/tuple of ops with kwargs. |
| 143 | if not isinstance(list_ops_or_scope, (list, tuple)): |
| 144 | raise TypeError('list_ops_or_scope must either be a list/tuple or reused ' |
| 145 | 'scope (i.e. dict)') |
| 146 | try: |
| 147 | current_scope = current_arg_scope().copy() |
| 148 | for op in list_ops_or_scope: |
| 149 | key = arg_scope_func_key(op) |
| 150 | if not has_arg_scope(op): |
| 151 | raise ValueError('%s is not decorated with @add_arg_scope', |
| 152 | _name_op(op)) |
| 153 | if key in current_scope: |
| 154 | current_kwargs = current_scope[key].copy() |
| 155 | current_kwargs.update(kwargs) |
| 156 | current_scope[key] = current_kwargs |
| 157 | else: |
| 158 | current_scope[key] = kwargs.copy() |
| 159 | _get_arg_stack().append(current_scope) |
| 160 | yield current_scope |
| 161 | finally: |
| 162 | _get_arg_stack().pop() |
| 163 | |
| 164 | |
| 165 | def add_arg_scope(func): |