gpt4 book ai didi

tensorflow - 如何在tensorflow中正确使用tf.layers.batch_normalization()?

转载 作者:行者123 更新时间:2023-12-03 00:41:17 25 4
gpt4 key购买 nike

我对 tensorflow 中的 tf.layers.batch_normalization 感到困惑。

我的代码如下:

def my_net(x, num_classes, phase_train, scope):
x = tf.layers.conv2d(...)
x = tf.layers.batch_normalization(x, training=phase_train)
x = tf.nn.relu(x)
x = tf.layers.max_pooling2d(...)

# some other staffs
...

# return
return x

def train():
phase_train = tf.placeholder(tf.bool, name='phase_train')
image_node = tf.placeholder(tf.float32, shape=[batch_size, HEIGHT, WIDTH, 3])
images, labels = data_loader(train_set)
val_images, val_labels = data_loader(validation_set)
prediction_op = my_net(image_node, num_classes=2,phase_train=phase_train, scope='Branch1')

loss_op = loss(...)
# some other staffs
optimizer = tf.train.AdamOptimizer(base_learning_rate)
update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS)
with tf.control_dependencies(update_ops):
train_op = optimizer.minimize(loss=total_loss, global_step=global_step)
sess = ...
coord = ...
while not coord.should_stop():
image_batch, label_batch = sess.run([images, labels])
_,loss_value= sess.run([train_op,loss_op], feed_dict={image_node:image_batch,label_node:label_batch,phase_train:True})

step = step+1

if step==NUM_TRAIN_SAMPLES:
for _ in range(NUM_VAL_SAMPLES/batch_size):
image_batch, label_batch = sess.run([val_images, val_labels])
prediction_batch = sess.run([prediction_op], feed_dict={image_node:image_batch,label_node:label_batch,phase_train:False})
val_accuracy = compute_accuracy(...)


def test():
phase_train = tf.placeholder(tf.bool, name='phase_train')
image_node = tf.placeholder(tf.float32, shape=[batch_size, HEIGHT, WIDTH, 3])
test_images, test_labels = data_loader(test_set)
prediction_op = my_net(image_node, num_classes=2,phase_train=phase_train, scope='Branch1')

# some staff to load the trained weights to the graph
saver.restore(...)

for _ in range(NUM_TEST_SAMPLES/batch_size):
image_batch, label_batch = sess.run([test_images, test_labels])
prediction_batch = sess.run([prediction_op], feed_dict={image_node:image_batch,label_node:label_batch,phase_train:False})
test_accuracy = compute_accuracy(...)

训练似乎效果很好,并且 val_accuracy 是合理的(比如 0.70)。问题是:当我尝试使用训练好的模型进行测试(即 test 函数)时,如果 phase_train 设置为 Falsetest_accuracy 非常低(例如 0.000270),但是当 phase_train 设置为 True 时, test_accuracy 似乎是正确的(例如 0.69)。

据我了解,在测试阶段phase_train应该是False,对吗?我不确定问题是什么。我是否误解了批量归一化?

最佳答案

这可能是您的代码中的一些错误,或者只是过度拟合。如果您对训练数据进行评估,准确性是否与训练期间一样高?如果问题出在批量归一化上,那么在没有训练的情况下,训练误差会比在训练模式下更高。如果问题是过度拟合,那么批量归一化可能不会导致该问题,根本原因在其他地方。

关于tensorflow - 如何在tensorflow中正确使用tf.layers.batch_normalization()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46573345/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com