Keras Model.fit Verbose Formatting

ぃ、小莉子 提交于 2020-06-24 06:52:13

问题


I'm running Keras model.fit() in Jupyter notebook, and the output is very messy if verbose is set to 1:

    Train on 6400 samples, validate on 800 samples
    Epoch 1/200
    2080/6400 [========>.....................] - ETA: 39s - loss: 0.4383 - acc: 0.79 
    - ETA: 34s - loss: 0.3585 - acc: 0.84 - ETA: 33s - loss: 0.3712 - acc: 0.84 
    - ETA: 34s - loss: 0.3716 - acc: 0.84 - ETA: 33s - loss: 0.3675 - acc: 0.84 
    - ETA: 33s - loss: 0.3650 - acc: 0.84 - ETA: 34s - loss: 0.3759 - acc: 0.83 
    - ETA: 34s - loss: 0.3933 - acc: 0.82 - ETA: 34s - loss: 0.3985 - acc: 0.82 
    - ETA: 34s - loss: 0.4057 - acc: 0.82 - ETA: 33s - loss: 0.4071 - acc: 0.81 
    ....

As you can see, the ETA, loss, acc outputs kept appending to the log, instead of replacing the original ETA/loss/acc values within the first line, just like how the progress bar works.

How do I fix it it so that only 1 line of progress bar, ETA, loss & acc are shown per epoch? Right now, my cell output has tons of these lines as the training continues.

I'm running Python 3.6.1 on Windows 10, with the following module versions:

jupyter                            1.0.0
jupyter-client                     5.0.1
jupyter-console                    5.1.0
jupyter-core                       4.3.0
jupyterthemes                      0.19.0
Keras                              2.2.0
Keras-Applications                 1.0.2
Keras-Preprocessing                1.0.1
tensorflow-gpu                     1.7.0

Thank you.


回答1:


You can try the Keras-adapted version of the TQDM progress bar library.

  • The original TQDM library: https://github.com/tqdm/tqdm
  • The Keras version of TQDM: https://github.com/bstriner/keras-tqdm

The usage instructions can be brought down to:

  1. install e.g. per pip install keras-tqdm (stable) or pip install git+https://github.com/bstriner/keras-tqdm.git (for latest dev-version)

  2. import the callback function with from keras_tqdm import TQDMNotebookCallback

  3. run Keras' fit or fit_generator with verbose=0 or verbose=2 settings, but with a callback to the imported TQDMNotebookCallback, e.g. model.fit(X_train, Y_train, verbose=0, callbacks=[TQDMNotebookCallback()])

The result:




回答2:


Took me a while to see this but tqdm (version >= 4.41.0) has also just added built-in support for keras so you could do:

from tqdm.keras import TqdmCallback
...
model.fit(..., verbose=0, callbacks=[TqdmCallback(verbose=2)])

This turns off keras' progress (verbose=0), and uses tqdm instead. For the callback, verbose=2 means separate progressbars for epochs and batches. 1 means clear batch bars when done. 0 means only show epochs (never show batch bars).

If there are any issues with it please feel free to post on https://github.com/tqdm/tqdm/issues



来源:https://stackoverflow.com/questions/52261597/keras-model-fit-verbose-formatting

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!