[英]How to reset initialization in TensorFlow 2
如果我在初始化tf.Variable
后嘗試更改 TensorFlow 2 中的tf.Variable
,
import tensorflow as tf
_ = tf.Variable([1])
tf.config.threading.set_inter_op_parallelism_threads(1)
我收到一個錯誤
運行時錯誤:初始化后無法修改操作間並行性。
我明白為什么會這樣,但它(可能還有其他因素)導致我的測試相互干擾。 例如
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)
...
如何重置 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)
編輯:一開始就設置配置是什么意思,
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
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.