前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >slim.arg_scope()

slim.arg_scope()

作者头像
狼啸风云
修改2022-09-04 21:24:04
6760
修改2022-09-04 21:24:04
举报
代码语言: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.
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019年08月19日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档