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