slim.arg_scope()

2022-09-04 21:24:04 浏览数 (1)

代码语言:javascript复制
def arg_scope(list_ops_or_scope, **kwargs):
  if isinstance(list_ops_or_scope, dict):
    # Assumes that list_ops_or_scope is a scope that is being reused.
    if kwargs:
      raise ValueError('When attempting to re-use a scope by suppling a'
                       'dictionary, kwargs must be empty.')
    current_scope = list_ops_or_scope.copy()
    try:
      _get_arg_stack().append(current_scope)
      yield current_scope
    finally:
      _get_arg_stack().pop()
  else:
    # Assumes that list_ops_or_scope is a list/tuple of ops with kwargs.
    if not isinstance(list_ops_or_scope, (list, tuple)):
      raise TypeError('list_ops_or_scope must either be a list/tuple or reused '
                      'scope (i.e. dict)')
    try:
      current_scope = current_arg_scope().copy()
      for op in list_ops_or_scope:
        key = arg_scope_func_key(op)
        if not has_arg_scope(op):
          raise ValueError('%s is not decorated with @add_arg_scope',
                           _name_op(op))
        if key in current_scope:
          current_kwargs = current_scope[key].copy()
          current_kwargs.update(kwargs)
          current_scope[key] = current_kwargs
        else:
          current_scope[key] = kwargs.copy()
      _get_arg_stack().append(current_scope)
      yield current_scope
    finally:
      _get_arg_stack().pop()

存储给定list_ops集合的默认参数。

参数:

  • list_ops_or_scope:为包含当前范围的字典设置参数范围的操作的列表或元组。当list_ops_or_scope是dict时,kwargs必须为空。当list_ops_or_scope是一个列表或元组时,其中的每个op都需要用@add_arg_scope修饰才能工作。
  • **kwargs: keyword=value,它将为list_ops中的每个操作定义默认值。所有的ops都需要接受给定的一组参数。**kwargs:current_scope是{op: {arg: value}}的字典。

返回值:

  • yield:current_scope是{op: {arg: value}}的字典

可能产生的异常:

代码语言:javascript复制
TypeError: if list_ops is not a list or a tuple.
ValueError: if any op in list_ops has not be decorated with @add_arg_scope.

0 人点赞