python - 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
被打印到控制台,然后抛出错误。我在这里想念什么?
解决方案
sys.stdout
(默认文件)使用的缓冲区在开始运行print()
之前可能不会被刷新。model.fit
您可以使用 强制刷新此缓冲区flush=True
。
print('The script has started', flush=True)
推荐阅读
- c - GNU Make gcc,选项顺序
- sql - 查询以列出同一表下引用同一列的所有索引
- excel - 当我在 Visual Basic 中创建一个空列表时,我收到一条语法错误消息
- asp.net - 将授权与策略应用于 asp.net 核心中命名空间内的所有控制器
- javascript - 如何使用在方法之外的方法中找到的这个变量?
- ms-access - 来自 MS Access 表单的电子邮件
- flutter - 按自定义属性排序列表,然后按 DateTime 属性
- linux - 将时间戳分配给文件名的命令
- python - Python:具有多个返回/输入值的流水线函数,还是使用 OOP?最佳实践?
- python - 用于图像分类问题的网格搜索 cv 中的内存问题