【发布时间】:2018-06-05 19:12:22
【问题描述】:
我最近开始使用 Tensorflow,并且一直在努力适应环境。这真是太棒了!但是,使用 tf.contrib.layers.batch_norm 进行批量标准化有点棘手。 现在,这是我正在使用的功能:
def batch_norm(x, phase):
return tf.contrib.layers.batch_norm(x,center = True, scale = True,
is_training = phase, updates_collections = None)
使用这个,我遵循了我在网上找到的大多数文档(也是问答),它使我得出以下结论:
1) is_training 应该设置为 True 用于训练和 false 用于测试。这是有道理的!训练时,我有收敛(误差
但是在测试期间,我的结果很糟糕(错误 > 90%),除非我将 (update collections = None) 作为参数添加到上面的批处理规范函数中。只有以它作为论据,测试才会给我预期的错误。
我也肯定会使用以下内容进行培训:
update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS)
with tf.control_dependencies(update_ops): # Ensures, Updating ops will perform before training
with tf.name_scope('Cross_Entropy'):
cross_entropy = tf.reduce_mean( # Implement Cross_Entropy to compute the softmax activation
tf.nn.softmax_cross_entropy_with_logits(labels=y_, logits=y_conv)) # Cross Entropy: True Output Labels (y_), Softmax output (y_conv)
tf.summary.scalar('cross_entropy', cross_entropy) # Graphical output Cross Entropy
with tf.name_scope('train'):
train_step = tf.train.AdamOptimizer(1e-2).minimize(cross_entropy) # Train Network, Tensorflow minimizes cross_entropy via ADAM Optimization
with tf.name_scope('Train_Results'):
with tf.name_scope('Correct_Prediction'):
correct_prediction = tf.equal(tf.argmax(y_conv, 1), tf.argmax(y_, 1)) # Check if prediction is wrong with tf.equal(CNN_result,True_result)
with tf.name_scope('Accuracy'):
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) # Find the percent accuracy, take mean of correct_prediction outputs
tf.summary.scalar('accuracy', accuracy) # Graphical output Classification Accuracy
这应该确保批标准化参数在训练期间更新。
所以这让我相信 update collections = None 只是我的批量规范化函数的一个很好的默认值,在测试函数期间肯定不会调整任何批量规范化参数......我正确吗?
最后:在测试阶段打开和关闭批量标准化时,获得良好结果(预期错误)是否正常?使用上面的批处理规范函数,我能够很好地训练(is_training = True)并很好地测试(is_training = False)。但是,在测试期间(is_training = True),我仍然能够获得很好的结果。这只是给我一种不好的感觉。有人可以解释为什么会这样吗?或者它是否应该发生?
感谢您的宝贵时间!
【问题讨论】:
标签: tensorflow machine-learning deep-learning conv-neural-network