【问题标题】:How to predict after each epoch of training in Keras?如何在 Keras 的每个训练阶段之后进行预测?
【发布时间】:2018-09-28 02:16:09
【问题描述】:

我想在每个训练周期后可视化并保存验证数据的预测。在某种程度上,我可以进一步使用预测来离线分析它们。

我知道keras 的回调功能可能会起作用,但我想了解如何在model.fit() 中使用它。

【问题讨论】:

标签: python tensorflow machine-learning keras callback


【解决方案1】:

您可以编写自己的 callback 函数,然后在您的 model_fit()方法

查看 Keras 官方文档here:

class LossHistory(keras.callbacks.Callback):
    def on_train_begin(self, logs={}):
        self.losses = []

    def on_batch_end(self, batch, logs={}):
        self.losses.append(logs.get('loss'))

model = Sequential()
model.add(Dense(10, input_dim=784, kernel_initializer='uniform'))
model.add(Activation('softmax'))
model.compile(loss='categorical_crossentropy', optimizer='rmsprop')

history = LossHistory()
model.fit(x_train, y_train, batch_size=128, epochs=20, verbose=0, callbacks=[history])

print(history.losses)
# outputs
'''
[0.66047596406559383, 0.3547245744908703, ..., 0.25953155204159617, 0.25901699725311789]
'''

显然不是保存损失并追加它们。您也可以致电model.predict() 并将结果保存在您自己的callback 中。

【讨论】:

    猜你喜欢
    • 2019-09-06
    • 2018-04-15
    • 2021-04-22
    • 2017-07-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-27
    • 2021-02-22
    相关资源
    最近更新 更多