keras fit vs keras评估

时间:2018-07-07 06:56:47

标签: machine-learning keras conv-neural-network face-recognition

应该有人真的可以澄清这一点。

以下是Keras文档中的一些初始信息: Keras中的 fit 函数仅针对给定数量的纪元训练模型。 evaluate 函数返回测试模式下模型的损失值和指标值。

因此,两个函数都返回损失。为了举例说明,如果我有一个训练示例,则在每个训练步骤后,我从拟合函数中获得的损失应与从评估函数(在相同的训练步骤后)中获得的损失相同。 (这里的假设是,我在同一训练集上同时运行 fit evaluate 函数(仅包含1个示例)。

我的网络定义如下:

def identity_loss(y_true, y_pred):
    return K.mean(y_pred - 0 * y_true)

model = ResNet50(weights='imagenet')
model.layers.pop()
x = model.get_layer('flatten_1').output # layer 'flatten_1' is the last layer of the model
model_out = Dense(128, activation='relu',  name='model_out')(x)
model_out = Lambda(lambda  x: K.l2_normalize(x,axis=-1))(model_out)

new_model = Model(inputs=model.input, outputs=model_out)

anchor_input = Input(shape=(224, 224, 3), name='anchor_input')
pos_input = Input(shape=(224, 224, 3), name='pos_input')
neg_input = Input(shape=(224, 224, 3), name='neg_input')

encoding_anchor   = new_model(anchor_input)
encoding_pos      = new_model(pos_input)
encoding_neg      = new_model(neg_input)

loss = Lambda(triplet_loss)([encoding_anchor, encoding_pos, encoding_neg])
siamese_network = Model(inputs  = [anchor_input, pos_input, neg_input], 
                        outputs = loss) 
siamese_network.compile(loss=identity_loss, optimizer=Adam(lr=.00003))

后来,我使用拟合功能训练我的火车组(仅包含1个示例),历时10个纪元。为了检查拟合和评估函数之间的差异,我还在每个纪元的拟合函数之后立即运行评估函数,输出看起来像下面这样:

nr_epoch:  0 

Epoch 1/1
1/1 [==============================] - 4s 4s/step - loss: 2.0035
1/1 [==============================] - 3s 3s/step
eval_score for train set:  2.0027356147766113

nr_epoch:  1 

Epoch 1/1
1/1 [==============================] - 1s 1s/step - loss: 1.9816
1/1 [==============================] - 1s 1s/step
eval_score for train set:  2.001833915710449

nr_epoch:  2 

Epoch 1/1
1/1 [==============================] - 1s 1s/step - loss: 1.9601
1/1 [==============================] - 1s 1s/step
eval_score for train set:  2.00126576423645

nr_epoch:  3 

Epoch 1/1
1/1 [==============================] - 1s 1s/step - loss: 1.9388
1/1 [==============================] - 1s 1s/step
eval_score for train set:  2.0009117126464844

nr_epoch:  4 

Epoch 1/1
1/1 [==============================] - 1s 1s/step - loss: 1.9176
1/1 [==============================] - 1s 1s/step
eval_score for train set:  2.000725746154785

nr_epoch:  5 

Epoch 1/1
1/1 [==============================] - 1s 1s/step - loss: 1.8964
1/1 [==============================] - 1s 1s/step
eval_score for train set:  2.0006520748138428

nr_epoch:  6 

Epoch 1/1
1/1 [==============================] - 1s 1s/step - loss: 1.8759
1/1 [==============================] - 1s 1s/step
eval_score for train set:  2.0006656646728516

nr_epoch:  7 

Epoch 1/1
1/1 [==============================] - 1s 1s/step - loss: 1.8555
1/1 [==============================] - 1s 1s/step
eval_score for train set:  2.0007567405700684

nr_epoch:  8 

Epoch 1/1
1/1 [==============================] - 1s 1s/step - loss: 1.8355
1/1 [==============================] - 1s 1s/step
eval_score for train set:  2.0009000301361084

nr_epoch:  9 

Epoch 1/1
1/1 [==============================] - 2s 2s/step - loss: 1.8159
1/1 [==============================] - 2s 2s/step
eval_score for train set:  2.001085042953491

如图所示, fit 函数(在每个纪元末)报告的损失正在减少。来自评估函数的损失并没有减少。

因此,难题是:如果我在1个单个训练示例上运行模型,我是否应该从拟合和评估同一时期的函数中看到相同的损失(在每个时期之后)?如果我继续训练,火车的损失在减少,但是来自评估函数的损失却保持在同一水平,并且不会减少

最后,这是我如何称呼拟合和评估函数:

z = np.zeros(len(anchor_path))

siamese_network.fit(x=[anchor_imgs, pos_imgs, neg_imgs], 
                    y=z, 
                    batch_size=batch_size, 
                    epochs=1, 
                    verbose=1, 
                    callbacks=None, 
                    validation_split=0.0, 
                    validation_data=None, 
                    shuffle=True, 
                    class_weight=None, 
                    sample_weight=None, 
                    initial_epoch=0, 
                    steps_per_epoch=None, 
                    validation_steps=None)

eval_score = siamese_network.evaluate(x=[anchor_imgs, pos_imgs, neg_imgs], 
                                      y=z,
                                      batch_size = batch_size, 
                                      verbose = 1)
print('eval_score for train set: ', eval_score)

那么,为什么在fit函数执行过程中损失减少了,而评估函数却没有?我在哪里犯错了?

2 个答案:

答案 0 :(得分:1)

ResNet使用批处理归一化,在训练和测试过程中表现不一样。您认为应该从model.fitmodel.evaluate得到相同的训练损失的假设是错误的。

答案 1 :(得分:0)

通过进一步的研究(通过使用不同的关键字进行搜索),我发现了以下信息,这些信息也提供了解决方案。看来,许多人一直在遭受这个问题的困扰,尤其是在尝试利用迁移学习时。

以下是该问题的讨论和解决方案: Strange behaviour of the loss function in keras model, with pretrained convolutional base

这是有关此主题的博客文章: http://blog.datumbox.com/the-batch-normalization-layer-of-keras-is-broken/

很不幸,我认为Tensorflow和Keras都有非常糟糕的文档。