简体   繁体   English

传入会话时,Tensorflow Multiprocessing pickle rlock 错误

[英]Tensorflow Multiprocessing pickle rlock error when passing in session

I created a very simple Tensorflow program to test out multiprocessing (shown below), but I keep getting the error:我创建了一个非常简单的 Tensorflow 程序来测试多处理(如下所示),但我不断收到错误消息:

TypeError: Can't pickle _thread.Rlock objects类型错误:无法pickle _thread.Rlock 对象

I notice that my below program works perfectly if I don't pass in the tensorflow session, and instead just create it within the test_f2 function, so it must have something to do with passing in Tensorflow sessions in multiprocessing situations.我注意到,如果我不传入 tensorflow 会话,而只是在 test_f2 函数中创建它,我下面的程序可以完美运行,因此它必须与在多处理情况下传入 Tensorflow 会话有关。

Does anyone know how I can fix this?有谁知道我该如何解决这个问题? Thanks so much!非常感谢!

import tensorflow as tf
import multiprocessing
def test_f2(x, sess):
    import tensorflow as tf
    a = tf.Variable(x, name='a')
    b = tf.Variable(100, name='b')
    c = tf.multiply(a, b, name='c')
    sess.run(tf.global_variables_initializer())
    out = sess.run(c)
    sess.close()
    print("OK: %s" % out)

if __name__ == '__main__':
    num_procs = 2
    procs_list = []
    for i in range(num_procs):
        new_session = tf.Session()
        p = multiprocessing.Process(target=test_f2, args=(i, new_session))
        p.daemon = True
        p.start()
        procs_list.append(p)
    for p in procs_list:
        p.join()

I'm using Tensorflow-GPU 1.14.0 and Python 3.7.4.我正在使用 Tensorflow-GPU 1.14.0 和 Python 3.7.4。

For starters, move all of your imports to the top of your file (there is still an import at the top of test_f2() .对于初学者,将所有导入移动到文件顶部( test_f2()顶部仍然有一个导入。

But the bigger issue is the way this is trying to share a session across multiple different processes.但更大的问题是它试图跨多个不同进程共享会话的方式。 The session is dependent on the memory addressing in the parent process, which the child processes cannot access ( multiprocessing is true fork and exec type process spawning, it is not threading and the children do not share memory space).会话依赖于父进程中的内存寻址,子进程无法访问( multiprocessing是真正的forkexec类型进程产生,它不是线程化并且子进程不共享内存空间)。 You need to create the session in the child processes, it doesn't work to share the way you have it coded.您需要在子进程中创建会话,共享您编码的方式是行不通的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM