Keras verbose training progress bar writing a new line on each batch issue

情到浓时终转凉″ 提交于 2019-11-29 03:42:29

This seems to be a consistent problem with Keras. I tried to find the lines

sys.stdout.write('\b' * prev_total_width)

sys.stdout.write('\r')

at the Keras/utils/generic_utils.py file and they are (as of the current version) at 258 and 259 accordingly. I commented like 258 but this seems not to resolve the issue. I did manage to make the progress bar work by commenting the line:

line 303: sys.stdout.write(info)

It seems as if the info makes the bar too long for the terminal and so it breaks to a new line.

So I finally resolved the issue. It seems like it was rather simple at the end....

Just make the terminal wider...

Note: Tested on Linux Ubuntu 16.04 | Keras Version 2.0.5

It was mentioned before, but I will rewrite it to be more visible for future users.

You have too narrow terminal to print all these values - just set width argument of Progbar constructor to smaller number or remove/rename some of the provided values.

I had a similar issue, but have not had the time to investigate it further. The problem seems to be related to the class Progbar in generic_utils.py of keras, see link, and perhaps Python >= 3.3.

The following lines are found in the update function of the class:

Line 107: sys.stdout.write('\b' * prev_total_width)
Line 108: sys.stdout.write('\r')

I simply removed line 107 as a quick fix, so instead of backspacing the previous line then performing a shift to the beginning of the line, I only perform the shift. I guess there is some better ways than altering the source code though.

The workaround of androst (4 Jan 2017) did not work for me. However, I found out that the lines of code which androst cited from generic_utils.py were never executed when I ran my code, because the condition in the embracing "if-clause" was always False. When I out-commented the if-clause, and set the corresponding variable (manually) to "True", it worked.

This is what I changed (for me: lines 311-314 of generic_utils.py):

    #self._dynamic_display = ((hasattr(sys.stdout, 'isatty') and
    #                          sys.stdout.isatty()) or
    #                         'ipykernel' in sys.modules)
    self._dynamic_display = True   # inserted to overwrite the above (workaround by KS)

Then the progress bar worked nicely :-)

The solutions of @user11353683 and @Panos can be completed in order to not modify the sources (which can create future problems): just install ipykernel and import it in your code:

pip install ipykernel Then import ipykernel

In fact, in the Keras generic_utils.py file, the probematic line was:

            if self._dynamic_display:
                sys.stdout.write('\b' * prev_total_width)
                sys.stdout.write('\r')
            else:
                sys.stdout.write('\n')

And the value self._dynamic_display was initiated such as:

        self._dynamic_display = ((hasattr(sys.stdout, 'isatty') and
                                  sys.stdout.isatty()) or
                                 'ipykernel' in sys.

So, loading ipykerneladded it to sys.modules and fixed the problem for me.

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