Background:
I have some complex reinforcement learning algorithm that I want to run in multiple threads.
Problem
When trying to call sess.run
in a thread I get the following error message:
RuntimeError: The Session graph is empty. Add operations to the graph before calling run().
Code reproducing the error:
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)
If I run the same code outside a thread it works properly.
Can someone give some insight on how to use Tensorflow sessions properly with python threads?