Joe*_*ley 6 python pytest tensorflow tensorflow2.0
如果我在初始化 a 后尝试更改 TensorFlow 2 中的并行性tf.Variable,
import tensorflow as tf
_ = tf.Variable([1])
tf.config.threading.set_inter_op_parallelism_threads(1)
Run Code Online (Sandbox Code Playgroud)
我收到一个错误
运行时错误:初始化后无法修改操作间并行性。
我明白为什么会这样,但它(可能还有其他因素)导致我的测试相互干扰。例如
def test_model(): # this test
v = tf.Variable([1])
...
def test_threading(): # is breaking this test
tf.config.threading.set_inter_op_parallelism_threads(1)
...
Run Code Online (Sandbox Code Playgroud)
如何重置 TensorFlow 状态以便设置线程?
这可以通过“hacky”方式实现。但我建议以正确的方式执行此操作(即通过在开始时设置配置)。
import tensorflow as tf
from tensorflow.python.eager import context
_ = tf.Variable([1])
context._context = None
context._create_context()
tf.config.threading.set_inter_op_parallelism_threads(1)
Run Code Online (Sandbox Code Playgroud)
编辑:一开始设置配置是什么意思,
import tensorflow as tf
from tensorflow.python.eager import context
tf.config.threading.set_inter_op_parallelism_threads(1)
_ = tf.Variable([1])
Run Code Online (Sandbox Code Playgroud)
但在某些情况下,您可能无法始终这样做。仅指出在tf. 因此,如果您的情况不允许您tf.config一开始就修复,您必须tf.eager.context按照上面的解决方案进行重置。
| 归档时间: |
|
| 查看次数: |
1631 次 |
| 最近记录: |