如何正确使用tensorflow的tf.layers.batch_normalization()?

问题描述 投票:8回答:1

我在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设置为False,该test_accuracy非常低(比如,0.000270),但是当phase_train设置为True中,test_accuracy似乎是正确的(比如说0.69)。

据我了解,phase_train应在测试阶段False,对不对?我不知道是什么问题。难道我误解了批标准化?

tensorflow batch-normalization
1个回答
0
投票

这可能是在你的代码的一些bug,或者只是过度拟合。如果你在列车上的数据评估,是精度高达训练中,还是没有?如果问题是与一批规范,然后火车错误将没有在训练模式,然后培养更高。如果问题是过学习,然后批量标准可能并没有引起这一点,根本原因是在其他地方。

© www.soinside.com 2019 - 2024. All rights reserved.