在多个线程中重用Tensorflow会话会导致崩溃 [英] Reusing Tensorflow session in multiple threads causes crash

查看:916
本文介绍了在多个线程中重用Tensorflow会话会导致崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

背景:

我有一些复杂的强化学习算法,我想在多个线程中运行.

I have some complex reinforcement learning algorithm that I want to run in multiple threads.

问题

尝试在线程中调用sess.run时,出现以下错误消息:

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().

代码重现该错误:

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.

有人可以提供一些关于如何在python线程中正确使用Tensorflow会话的见解吗?

Can someone give some insight on how to use Tensorflow sessions properly with python threads?

推荐答案

Session不仅可以是当前线程的默认值,而且还可以是图形的默认值. 当您传递会话并在其上调用run时,默认图形将是另一个图形.

Not only can the Session be the current thread default, but also the graph. While you pass in the session and call run on it, the default graph will be a different one.

您可以像这样修改 thread_function 使其起作用:

You can ammend your thread_function like this to make it work:

def thread_function(sess, i):
    with sess.graph.as_default():
        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})

但是,我不希望有任何明显的提速. Python线程在某些其他语言中并不是什么意思,只有某些操作(例如io)可以并行运行.对于CPU繁重的操作,它不是很有用.多处理可以真正地并行运行代码,但是您不会共享同一会话.

However, I wouldn't hope for any significant speedup. Python threading isn't what it means in some other languages, only certain operations, like io, would run in parallel. For CPU heavy operations it's not very useful. Multiprocessing can run code truely in parallel, but you wouldn't share the same session.

这篇关于在多个线程中重用Tensorflow会话会导致崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆