首页 > 解决方案 > TensorFlow 在脚本执行之前就知道脚本内部存在函数,怎么做?

问题描述

我使用的是 TensorFlow 2.3,安装的 GPU 库有问题,所以我的脚本没有运行。这不是这里的问题,相反,发生的奇怪事情是当脚本中存在某个 TensorFlow 函数时,脚本没有运行。例如:

# test.py
print('The script has started')
import tensorflow as tf
model = tf.keras.Model(...)
model.fit()

只要fit()函数存在,脚本就不会被执行,甚至顶部的 print 语句也不会执行;相反,错误只是打印到控制台。然而,一旦我删除了这个fit()函数,脚本就运行得很好。TensorFlow 如何知道该fit()函数存在于代码中?我通常希望The script has started被打印到控制台,然后抛出错误。我在这里想念什么?

标签: pythonpython-3.xtensorflowtensorflow2.0

解决方案


sys.stdout(默认文件)使用的缓冲区在开始运行print()之前可能不会被刷新。model.fit您可以使用 强制刷新此缓冲区flush=True

print('The script has started', flush=True)

推荐阅读