TensorFlow 在脚本执行之前就知道函数存在于脚本内部,怎么办?

TensorFlow knows function exists inside of script before script is executed, how?

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

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

只要 fit() 函数出现,脚本就不会执行,甚至顶部的打印语句也不执行;相反,错误只是打印到控制台。然而,一旦我删除了 fit() 函数,脚本 运行 就完全没问题了。 TensorFlow 如何知道代码中存在 fit() 函数?我通常希望将 The script has started 打印到控制台,然后抛出错误。我在这里错过了什么?

sys.stdoutprint() 的默认文件)使用的缓冲区可能在 model.fit 启动 运行 之前未刷新。您可以使用 flush=True.

强制刷新此缓冲区
print('The script has started', flush=True)