如何在TensorFlow 2中重置初始化

huangapple go评论70阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2020年1月7日 01:27:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/59616436.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定