英文:
How to reset initialization in TensorFlow 2
问题
If I try to change parallelism in TensorFlow 2 after initializing a tf.Variable
,
import tensorflow as tf
_ = tf.Variable([1])
tf.config.threading.set_inter_op_parallelism_threads(1)
I get an error
RuntimeError: Inter op parallelism cannot be modified after initialization.
I understand why that could be, but it (and possibly other factors) are causing my tests to interfere with each other. For example
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)
...
How do I reset the TensorFlow state so that I can set the threading?
英文:
If I try to change parallelism in TensorFlow 2 after initializing a tf.Variable
,
import tensorflow as tf
_ = tf.Variable([1])
tf.config.threading.set_inter_op_parallelism_threads(1)
I get an error
> RuntimeError: Inter op parallelism cannot be modified after initialization.
I understand why that could be, but it (and possibly other factors) are causing my tests to interfere with each other. For example
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)
...
How do I reset the TensorFlow state so that I can set the threading?
答案1
得分: 3
这是可以通过一种"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)
编辑: 在开始时设置配置意味着什么,
import tensorflow as tf
from tensorflow.python.eager import context
tf.config.threading.set_inter_op_parallelism_threads(1)
_ = tf.Variable([1])
但可能会有情况下,你不能总是这样做。只是指出在tf
中设置配置的传统方式。所以如果你的情况不允许你在开始时固定tf.config
,你必须像上面的解决方案中所示重新设置你的tf.eager.context
。
英文:
This is achievable in a "hacky" way. But I'd recommend doing this the right way (i.e. by setting up config at the beginning).
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)
Edit: What is meant by setting up config at the beginning,
import tensorflow as tf
from tensorflow.python.eager import context
tf.config.threading.set_inter_op_parallelism_threads(1)
_ = tf.Variable([1])
But there could be circumstances where you cannot always do this. Merely pointing out the conventional way of setting up config in tf
. So if your circumstances don't allow you to fix tf.config
at the beginning you have to reset your tf.eager.context
as shown in the solution above.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论