首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在tensorflow中正确使用tf.layers.batch_normalization()?

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

Stack Overflow用户
提问于 2017-10-05 04:08:20
回答 1查看 4.9K关注 0票数 10

我被tensorflow中的tf.layers.batch_normalization搞糊涂了。

我的代码如下:

代码语言:javascript
复制
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,对吧?我不确定问题出在哪里。我误解了批处理规范化吗?

EN

回答 1

Stack Overflow用户

发布于 2019-02-10 02:59:08

这可能是你的代码中的一些bug,或者仅仅是过度匹配。如果你在训练数据上进行评估,准确率是否和训练时一样高?如果问题是批量规范,那么没有训练的训练错误会比训练模式下的训练错误更高。如果问题是过度拟合,那么批处理规范可能不会导致这种情况,根本原因是其他地方。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46573345

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档