使用tf.layers

时间:2018-01-09 17:10:25

标签: tensorflow tensorflow-layers

我正在使用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'):

2 个答案:

答案 0 :(得分:2)

没有其他机制允许您在核心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()

答案 1 :(得分:1)

您可以使用 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])