2

我正在tf.contrib.slim.nets.inception_v3使用tf.layers. 不幸的是,新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'):
4

2 回答 2

3

没有其他机制可以让您在核心 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))

请务必阅读该层的文档以查看哪些初始化程序默认为变量范围初始化程序。例如,密集层使用变量范围初始化器kernel_initializer,而bias_initializer使用tf.zeros_initializer().

于 2018-02-06T02:20:17.790 回答
2

您可以使用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])
于 2018-10-09T06:14:26.177 回答