背景:
我有一些复杂的强化学习算法,我想在多个线程中运行。
问题
尝试在线程中调用sess.run
时,会出现以下错误消息:
RuntimeError: The Session graph is empty. Add operations to the graph before calling run().
代码重现错误:
import tensorflow as tf
import threading
def thread_function(sess, i):
inn = [1.3, 4.5]
A = tf.placeholder(dtype=float, shape=(None), name="input")
P = tf.Print(A, [A])
Q = tf.add(A, P)
sess.run(Q, feed_dict={A: inn})
def main(sess):
thread_list = []
for i in range(0, 4):
t = threading.Thread(target=thread_function, args=(sess, i))
thread_list.append(t)
t.start()
for t in thread_list:
t.join()
if __name__ == '__main__':
sess = tf.Session()
main(sess)
如果我在线程外运行相同的代码,它可以正常工作。
有人可以提供一些关于如何在Python线程中正确使用Tensorflow会话的见解吗?