使用 tf.layers 时替代 arg_scope

Alternative to arg_scope when using tf.layers

我正在使用 tf.layers 重写 tf.contrib.slim.nets.inception_v3。不幸的是,新的 tf.layers 模块不能与 arg_scope 一起使用,因为它没有必要的装饰器。是否有更好的机制可以用来设置图层的默认参数?或者我应该简单地为每一层添加一个适当的参数并删除 arg_scope?

这是一个使用 arg_scope:

的例子
with variable_scope.variable_scope(scope, 'InceptionV3', [inputs]):
    with arg_scope(
        [layers.conv2d, layers_lib.max_pool2d, layers_lib.avg_pool2d],
        stride=1,
        padding='VALID'):

没有其他机制可以让您在核心 TensorFlow 中定义默认值,因此您应该为每一层指定参数。

例如,这段代码:

with slim.arg_scope([slim.fully_connected], 
    activation_fn=tf.nn.relu, 
    weights_initializer=tf.truncated_normal_initializer(stddev=0.01),
    weights_regularizer=tf.contrib.layers.l2_regularizer(scale=0.0005)):
  x = slim.fully_connected(x, 800)
  x = slim.fully_connected(x, 1000)

会变成:

x = tf.layers.dense(x, 800, activation=tf.nn.relu,
      kernel_initializer=tf.truncated_normal_initializer(stddev=0.01),
      kernel_regularizer=tf.contrib.layers.l2_regularizer(scale=0.0005))
x = tf.layers.dense(x, 1000, activation=tf.nn.relu,
      kernel_initializer=tf.truncated_normal_initializer(stddev=0.01),
      kernel_regularizer=tf.contrib.layers.l2_regularizer(scale=0.0005))

或者:

with tf.variable_scope('fc', 
    initializer=tf.truncated_normal_initializer(stddev=0.01)):
  x = tf.layers.dense(x, 800, activation=tf.nn.relu,
      kernel_regularizer=tf.contrib.layers.l2_regularizer(scale=0.0005))
  x = tf.layers.dense(x, 1000, activation=tf.nn.relu,
      kernel_regularizer=tf.contrib.layers.l2_regularizer(scale=0.0005))

确保阅读该层的文档以查看哪些初始化器默认为变量范围初始化器。例如,dense layerkernel_initializer 使用变量范围初始值设定项,而 bias_initializer 使用 tf.zeros_initializer().

您可以使用 tensorflow.contrib.framework 中的 add_arg_scope,它添加了必要的装饰器并使函数可用于 arg_scope 。在 tf.layers.requiredLayer 周围创建一个包装器并用 @add_arg_scope.

装饰它

示例:

import tensorflow as tf
from tensorflow.contrib.framework import arg_scope
from tensorflow.contrib.framework import add_arg_scope

@add_arg_scope
def conv2d(inputs,filters,kernel_size,padding='VALID',activation=tf.nn.sigmoid):
    print inputs
    print filters
    print kernel_size
    print padding
    print activation
    return tf.layers.conv2d(
                  inputs=inputs,
                  filters=filters,
                  kernel_size=kernel_size,
                  padding=padding,
                  activation=activation)

inp = tf.placeholder(tf.float32,[None,224,224,3])


print '--------net1-------------'
with arg_scope([conv2d],padding='SAME',activation=tf.nn.relu):
    net = conv2d(inputs=inp,filters=64,kernel_size=[1,1])
    #print net
    #net=net
print '--------net2-------------'
net2 = conv2d(inputs=inp,filters=64,kernel_size=[1,1])