sys.stdout.flush在jupyter笔记本中不起作用 [英] sys.stdout.flush not work in jupyter notebook

查看:243
本文介绍了sys.stdout.flush在jupyter笔记本中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我只想重新运行此仓库中的代码: https://github.com/dennybritz/强化学习/blob/master/MC/MC%20Prediction%20Solution.ipynb

So I only want to re-run code from this repo: https://github.com/dennybritz/reinforcement-learning/blob/master/MC/MC%20Prediction%20Solution.ipynb

我的重点是印刷品:

 for i_episode in range(1, num_episodes + 1):
        # Print out which episode we're on, useful for debugging.
        if i_episode % 1000 == 0:
            print "\rEpisode {}/{}.".format(i_episode, num_episodes)
            sys.stdout.flush()

他正在使用sys.stdout.flush()创建简单的进度"输出.您可以看到他的仓库中的输出,它仅显示最后的情节迭代10000/10000,因为使用sys.stdout.flush()

He is using sys.stdout.flush() to create a simple "progress" output. You can see the output from his repo it only show the last Episode iteration 10000/10000 because using sys.stdout.flush()

但是当我尝试在jupyter笔记本中运行它(我使用cmd命令jupyter notebook运行)时,我认为sys.stdout.flush()无法正常工作,它会显示每个打印的迭代,而不会覆盖上一个:

But when I try to run it in my jupyter notebook (I run using cmd command jupyter notebook) I think sys.stdout.flush() not works, it show every printed iterations, not overwrite the previous one:

Episode 1000/10000.
Episode 2000/10000.
Episode 3000/10000.
Episode 4000/10000.
Episode 5000/10000.
Episode 6000/10000.
Episode 7000/10000.
Episode 8000/10000.
Episode 9000/10000.
Episode 10000/10000.

运行jupyter使它正常工作时,我是否缺少某些东西?

Am I missing something when run jupyter to make it works?

推荐答案

Charless Duffy的注释使所有内容变得清晰.覆盖的问题不在打印功能中的冲洗功能上有效.

Charless Duffy comment makes everything clear. The problem of overwriting is not on flush function buat in print function.

我发现解决方案只是从以下位置编辑打印格式:

I found the solution is just edit the print format from:

print("\rEpisode {}/{}.".format(i_episode, num_episodes))

print("\rEpisode {}/{}.".format(i_episode, num_episodes), end="")

并且覆盖现在可以正常工作

and the overwrite works now

这篇关于sys.stdout.flush在jupyter笔记本中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆