具有BatchNormalization的模型:停滞测试损失

时间:2017-04-27 09:57:11

标签: tensorflow keras keras-layer

我用Keras写了一个神经网络。它包含BatchNormalization图层。

当我用model.fit训练时,一切都很好。当使用张量流as explained here进行训练时,训练很好,但验证步骤总是表现不佳,并且很快就会饱和(准确度为5%,10%,40%,40%,40%...... ;损失也是停滞不前的)。

我需要使用tensorflow,因为它可以在培训的监控部分提供更大的灵活性。

我强烈怀疑它与BN图层有关或/和我计算测试性能的方式(见下文)

feed_dict = {x: X_valid,
            batch_size_placeholder: X_valid.shape[0],
            K.learning_phase(): 0,
            beta: self.warm_up_schedule(global_step)
            }
if self.weights is not None:
    feed_dict[weights] = self.weights
acc = accuracy.eval(feed_dict=feed_dict)

在计算包含Keras BatchNormalizatin图层的模型的验证准确度时,有什么特别的事情要做吗?

提前谢谢!

1 个答案:

答案 0 :(得分:0)

实际上我发现了__call__ method of the BatchNormalization layer

training参数

所以在实例化图层时你可以做的只是:

x = Input((dim1, dim2))
h = Dense(dim3)(x)
h = BatchNormalization()(h, training=K.learning_phase())

在评估验证集上的性能时:

feed_dict = {x: X_valid,
             batch_size_placeholder: X_valid.shape[0],
             K.learning_phase(): 0,
             beta: self.warm_up_schedule(global_step)
             }
acc = accuracy.eval(feed_dict=feed_dict)
summary_ = merged.eval(feed_dict=feed_dict)
test_writer.add_summary(summary_, global_step)
相关问题