缘由

  最近一直在看深度学习的代码,又一次看到了slim.arg_scope()的嵌套使用,具体代码如下:

with slim.arg_scope(
      [slim.conv2d, slim.separable_conv2d],
      weights_initializer=tf.truncated_normal_initializer(
          stddev=weights_initializer_stddev),
      activation_fn=activation_fn,
      normalizer_fn=slim.batch_norm if use_batch_norm else None):
    with slim.arg_scope([slim.batch_norm], **batch_norm_params):
      with slim.arg_scope(
          [slim.conv2d],
          weights_regularizer=slim.l2_regularizer(weight_decay)):
        with slim.arg_scope(
            [slim.separable_conv2d],
            weights_regularizer=depthwise_regularizer) as arg_sc:
          return arg_sc

  由上述代码可以看到,第一层argscope有slim.conv2d参数,第三层也有这个参数,那么不同层的参数是如何相互补充,作用到之后的代码块中,就是这篇博文的出发点。

准备工作

  我们先看一下arg_scope的函数声明:

@tf_contextlib.contextmanager
def arg_scope(list_ops_or_scope, **kwargs):

  有函数修饰符@tf_contextlib.contextmanager修饰arg_scope函数,我们先研究下这个函数修饰符。

@的作用

  @之后一般接一个可调用对象(tf_contextlib.contextmanager),一起构成函数修饰符(装饰器),这个可调用对象将被修饰函数(arg_scope)作为参数,为其执行一系列辅助操作,我们来看一个demo

import time

def my_time(func):
    print(time.ctime())
    return func()

@my_time  # 从这里可以看出@time 等价于 time(xxx()),但是这种写法你得考虑python代码的执行顺序
def xxx():
    print('Hello world!')

运行结果:
Wed Jul 26 23:01:21 2017
Hello world!

  在这个例子中,xxx函数实现我们的主要功能,打印Hello world!,但我们想给xxx函数添加一些辅助操作,让它同时打印出时间,于是我们用函数修饰符@my_time完成这个目标。整个例子的执行流程为调用my_time可调用对象,它接受xxx函数作为参数,先打印时间,再执行xxx函数。

上下文管理器

  既然arg_scope函数存在装饰器,那么我们应该了解一下@tf_contextlib.contextmanager装饰器提供了什么辅助功能,代码为:

import contextlib as _contextlib

from tensorflow.python.util import tf_decorator

def contextmanager(target):
  """A tf_decorator-aware wrapper for `contextlib.contextmanager`.
  Usage is identical to `contextlib.contextmanager`.
  Args:
    target: A callable to be wrapped in a contextmanager.
  Returns:
    A callable that can be used inside of a `with` statement.
  """
  context_manager = _contextlib.contextmanager(target)
  return tf_decorator.make_decorator(target, context_manager, 'contextmanager') #会在下一篇介绍这个return语句的功能

上下文管理器——contextlib库

  可以看到导入了contextlib库,这个库提供了contextmanager函数,这也是一个装饰器,它使被修饰的函数具有上下文管理器的功能。上下文管理器使我们能在执行一段代码块之前做一些准备工作,执行完代码块之后做一些收尾工作,同样先来看一个上下文管理器的例子:

import time

class MyTimer(object):
    def __init__(self, verbose = False):
        self.verbose = verbose

    def __enter__(self):
        self.start = time.time()
        return self

    def __exit__(self, *unused):
        self.end = time.time()
        self.secs = self.end - self.start
        self.msecs = self.secs * 1000
        if self.verbose:
            print "elapsed time: %f ms" %self.msecs

with MyTimer(True):  print('Hello world!')

  类MyTimer中的__enter__和__exit__方法分别是准备工作和收尾工作。整个代码的执行过程为:先执行__enter__方法,__enter__方法中的返回值(这个例子中是self)可以用到代码块中,再执行语句块,这个例子中是print函数,最后执行__exit__方法,更多关于上下文管理器的内容可以看,我的例子也是从那copy的。contextlib中实现上下文管理器稍有不同,一样来看个例子:

from contextlib import contextmanager

@contextmanager
def tag(name):
    print "<%s>" % name
    yield
    print "</%s>" % name

>>> with tag("h1"):
...    print "foo"
运行结果:
<h1>
foo
</h1>

  tag函数中yield之前的代码相当于__enter__方法,yield产生的生成器相当于__enter__方法的返回值,yield之后的代码相当于__exit__方法。

arg_scope方法

  这里我把arg_scope方法中代码稍微做了一些精简,代码如下:

arg_scope = [{}]

@tf_contextlib.contextmanager
def arg_scope(list_ops_or_scope, **kwargs):   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): # op是否用@slim.add_arg_scope修饰,这会在下一篇中介绍
          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()

# demo
with slim.arg_scope(
      [slim.conv2d, slim.separable_conv2d],
      weights_initializer=tf.truncated_normal_initializer(
          stddev=weights_initializer_stddev),
      activation_fn=activation_fn,
      normalizer_fn=slim.batch_norm if use_batch_norm else None):
    with slim.arg_scope([slim.batch_norm], **batch_norm_params):
      with slim.arg_scope(
          [slim.conv2d],
          weights_regularizer=slim.l2_regularizer(weight_decay)):
        with slim.arg_scope(
            [slim.separable_conv2d],
            weights_regularizer=depthwise_regularizer) as arg_sc:
          return arg_sc

第一层

  之后的层的处理就很类似,留给大家思考。

结语

  回到我们开头提到的问题,不同层的参数是如何互相补充的?现在我们可以看到,参数存储在栈中,每叠加一层,就在原有参数基础上把新参数添加上去。

                                            最后编辑于15:46:26 2018-07-24

tf.contrib.slim arg_scope的更多相关文章

  1. tf.contrib.slim模块简介

    原文连接:https://blog.csdn.net/MOU_IT/article/details/82717745 1.简介 对于tensorflow.contrib这个库,tensorflow官方 ...

  2. tf.contrib.slim add_arg_scope

    上一篇文章中我们介绍了arg_scope函数,它在每一层嵌套中update当前字典中参数形成新的字典,并入栈.那么这些参数是怎么作用到代码块中的函数的呢?比如说如下情况: with slim.arg_ ...

  3. tf.contrib.slim.data数据加载(1) reader

    reader: 适用于原始数据数据形式的Tensorflow Reader 在库中parallel_reader.py是与reader相关的,它使用多个reader并行处理来提高速度,但文件中定义的类 ...

  4. tf.contrib.slim.data数据加载 综述

    TF-Slim为了方便加载各种数据类型(如TFRocords或者文本文件)的数据,创建了这个库. Dataset 这里的数据库与通常意义下数据库是不同的,这里数据库是python一个类,它负责将原始数 ...

  5. tf.contrib.slim

    https://blog.csdn.net/mao_xiao_feng/article/details/73409975

  6. 图融合之加载子图:Tensorflow.contrib.slim与tf.train.Saver之坑

    import tensorflow as tf import tensorflow.contrib.slim as slim import rawpy import numpy as np impor ...

  7. slim.arg_scope()的使用

    [https://blog.csdn.net/u013921430 转载] slim是一种轻量级的tensorflow库,可以使模型的构建,训练,测试都变得更加简单.在slim库中对很多常用的函数进行 ...

  8. 使用多块GPU进行训练 1.slim.arg_scope(对于同等类型使用相同操作) 2.tf.name_scope(定义名字的范围) 3.tf.get_variable_scope().reuse_variable(参数的复用) 4.tf.py_func(构造函数)

    1. slim.arg_scope(函数, 传参) # 对于同类的函数操作,都传入相同的参数 from tensorflow.contrib import slim as slim import te ...

  9. 学习笔记TF044:TF.Contrib组件、统计分布、Layer、性能分析器tfprof

    TF.Contrib,开源社区贡献,新功能,内外部测试,根据反馈意见改进性能,改善API友好度,API稳定后,移到TensorFlow核心模块.生产代码,以最新官方教程和API指南参考. 统计分布.T ...

随机推荐

  1. 小程序 <web-view></web-view> 中使用 form 表单提交

    在最近的小程序项目中,使用到了 <web-view></web-view> 内嵌 H5 页面,在 H5 中需要使用 form 表单提交数据. H5 使用的技术框架是 vue+v ...

  2. python面试题一个字符串是否由重复的子字符串组成

    一,给定一个非空的字符串,判断它是否可以由它的一个子串重复多次构成.给定的字符串只含有小写英文字母,并且长度不超过10000. 输入: "abab" 输出: True 解释: 可由 ...

  3. python zip dict函数

    1.zip函数 zip函数可以接受多个参数,返回的结果是列表,列表中的每一个元素是元组的数据类型,下面我们通过几个例子来学习zip函数的用法 1) list1 = [1,2,3] list2 = [4 ...

  4. 初识正则表达式matcher.group

    matcher.group中group是匹配()的,group(0)指的是整个串,group(1) 指的是第一个括号里的内容,group(2)指的第二个括号里的内容,以此类推. 例如: str = & ...

  5. E: Unable to locate package clang-7 E: Unable to locate package clang++-7 E: Couldn't find any package by regex 'clang++-7'

    我的系统是Debian 9.8, 然后在装下面这两个包的时候老是提示找不到.然后再github上一问,过了几分钟就有大佬回复了,而且亲测有效 ~$ sudo apt-get -y install cl ...

  6. Python 并发编程

    进程 开启进程 from multiprocessing import Process import time def task(name): print('%s is running' %name) ...

  7. memcached单机或热备的安装部署

    一.部署准备 1.安装Java 不建议使用系统默认Open JDK版本,需要手工另行安装.JDK版本建议为1.7+,若Java已安装完毕,则无需重复安装. 安装过程如下: (1)获取JDK安装包: ( ...

  8. django会话

    django会话 可以把会话理解为客户端与服务器之间的一次会晤,在一次会话过程中有多次请求和响应,但是由于HTTP协议的特性-->无状态,每次浏览器的请求都是无状态的,无法保存状态信息,也就是说 ...

  9. 初学python之路-day12

    本篇补上字符串的比较:按照从左往右比较每一个字符,通过字符对应的ascii进行比较 一.函数默认值的细节 # 如果函数的默认参数的默认值为变量,在所属函数定义阶段一执行就被确定为当时变量存放的值 a ...

  10. 洛谷 P2678 & [NOIP2015提高组] 跳石头

    题目链接 https://www.luogu.org/problemnew/show/P2678 题目背景 一年一度的“跳石头”比赛又要开始了! 题目描述 这项比赛将在一条笔直的河道中进行,河道中分布 ...