简体   繁体   English

Keras中每批后如何更新训练日志输出?

[英]How to update the training log output after each batch in Keras?

I am training my model in Keras using a tensorflow backend and Jupyter-Notebook.我正在使用 tensorflow 后端和 Jupyter-Notebook 在 Keras 中训练我的模型。 While the MNIST Example updates the output of the training log after each batch, my new model on a different dataset outputs a new value for each batch.虽然 MNIST 示例在每个批次之后更新训练日志的输出,但我在不同数据集上的新模型为每个批次输出一个新值。 Now rather than using verbose=2, I would like to see the value being updated after every batch.现在,我不想使用verbose=2,而是希望看到每批之后更新的值。

My fit function looks like this:我的拟合函数如下所示:

model.fit(X, y_train, validation_split=0.33, epochs=1, batch_size=200, verbose=1)

The output looks like this:输出如下所示:

    Train on 16415 samples, validate on 8085 samples
    Epoch 1/1
    16415/16415 [==============================] - 
ETA: 73s - loss: 9.0281 -acc: 0.44 - ETA: 49s - loss: 9.0271 - acc: 0.44 -  
ETA: 36s - loss: 8.7043 - acc: 0.46 - ETA: 33s - loss: 8.3979 - acc: 0.47 - 
ETA: 31s - loss: 8.3549 - acc: 0.48 - ETA: 29s - loss: 8.3011 - acc: 0.48 - 
ETA: 28s - loss: 8.1802 - acc: 0.49 - ETA: 27s - loss: 8.1220 - acc: 0.49 - 
ETA: 26s - loss: 8.0995 - acc: 0.49 - ETA: 26s - loss: 8.1178 - acc: 0.49 - 
ETA: 25s - loss: 8.1264 - acc: 0.49 - ETA: 24s - loss: 8.1274 - acc: 0.49 - 
ETA: 24s - loss: 8.0880 - acc: 0.49 - ETA: 23s - loss: 8.0860 - acc: 0.49 - 
ETA: 23s - loss: 8.0894 - acc: 0.49 - ETA: 22s - loss: 8.1303 - acc: 0.49 -
... 

However, I would like to see only one line that updates after each batch like so:但是,我希望只看到一行在每批之后更新,如下所示:

Epoch 1/1
        16415/16415 [==============================] - 
    ETA: 23s - loss: 9.0281 -acc: 0.44 - ETA: 22s - loss: 9.0271 - acc: 0.49

I can't find any option in the keras documentation besides setting verbose=2, but this does not update the log during training.除了设置verbose=2之外,我在keras文档中找不到任何选项,但这不会在训练期间更新日志。

You can use a LambdaCallback to call custom functions between batches and epochs.您可以使用LambdaCallback在批次和纪元之间调用自定义函数。

Use the on_batch_end parameter to pass the function to call:使用on_batch_end参数传递函数来调用:

from keras.callbacks import LambdaCallback

def batchOutput(batch, logs):

    print("Finished batch: " + str(batch))
    print(logs)

batchLogCallback = LambdaCallback(on_batch_end=batchOutput)

model.fit(x,y,....,callbacks=[batchLogCallback])

At the time of you writing this question, there wasn't a built-in solution.在您撰写此问题时,还没有内置解决方案。 You could subclass tf.keras.callbacks.TensorBoard and implement an on_batch_end method.您可以tf.keras.callbacks.TensorBoard并实现on_batch_end方法。 In 2018, it was added to Keras. 2018 年,它被添加到 Keras。

In Tensorflow you can use the update_freq parameter of the tf.keras.callbacks.TensorBoard class and set it to batch .在 Tensorflow 中,您可以使用tf.keras.callbacks.TensorBoard类的update_freq参数并将其设置为batch

Like so:像这样:

tensorboard_callback = tf.keras.callbacks.TensorBoard(update_freq='batch')
tf_model.fit(x, y, epochs=1, callbacks=[tensorboard_callback])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM