Tensorflow和多处理:通过会话 [英] Tensorflow and Multiprocessing: Passing Sessions

查看:179
本文介绍了Tensorflow和多处理:通过会话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近一直在从事一个使用神经网络进行虚拟机器人控制的项目.我使用tensorflow对其进行了编码,并且运行平稳.到目前为止,我使用顺序仿真来评估神经网络的性能,但是,我想并行运行多个仿真,以减少获取数据所需的时间.

I have recently been working on a project that uses a neural network for virtual robot control. I used tensorflow to code it up and it runs smoothly. So far, I used sequential simulations to evaluate how good the neural network is, however, I want to run several simulations in parallel to reduce the amount of time it takes to get data.

为此,我要导入python的multiprocessing包.最初,我将sess变量(sess=tf.Session())传递给将运行模拟的函数.但是,一旦我找到任何使用此sess变量的语句,该过程便会退出,而不会发出警告.经过四处寻找之后,我发现了以下两个帖子: Tensorflow:将会话传递给python多进程同时运行多个tensorflow会话

To do this I am importing python's multiprocessing package. Initially I was passing the sess variable (sess=tf.Session()) to a function that would run the simulation. However, once I get to any statement that uses this sess variable, the process quits without a warning. After searching around for a bit I found these two posts: Tensorflow: Passing a session to a python multiprocess and Running multiple tensorflow sessions concurrently

虽然它们之间有着高度的联系,但我仍无法弄清楚如何使其发挥作用.我尝试为每个单独的过程创建一个会话,并将神经网络的权重分配给它的可训练参数,但没有成功.我也尝试过将会话保存到文件中,然后在一个进程中加载​​它,但是那里也没有运气.

While they are highly related I haven't been able to figure out how to make it work. I tried creating a session for each individual process and assigning the weights of the neural net to its trainable parameters without success. I've also tried saving the session into a file and then loading it within a process, but no luck there either.

有人能够将一个会话(或会话的克隆)传递给多个进程吗?

Has someone been able to pass a session (or clones of sessions) to several processes?

谢谢.

推荐答案

您不能使用Python多重处理以直接方式将TensorFlow Session传递给multiprocessing.Pool,因为Session对象不能腌制(因为它可能会管理GPU内存和状态,所以它基本上是不可序列化的.)

You can't use Python multiprocessing to pass a TensorFlow Session into a multiprocessing.Pool in the straightfoward way because the Session object can't be pickled (it's fundamentally not serializable because it may manage GPU memory and state like that).

我建议使用 actor 对代码进行并行化,它们实际上是的并行计算类似物对象"及其用于管理分布式设置中的状态.

I'd suggest parallelizing the code using actors, which are essentially the parallel computing analog of "objects" and use used to manage state in the distributed setting.

Ray 是一个很好的框架.您可以定义一个Python类,该类管理TensorFlow Session,并公开一种用于运行模拟的方法.

Ray is a good framework for doing this. You can define a Python class which manages the TensorFlow Session and exposes a method for running your simulation.

import ray
import tensorflow as tf

ray.init()

@ray.remote
class Simulator(object):
    def __init__(self):
        self.sess = tf.Session()
        self.simple_model = tf.constant([1.0])

    def simulate(self):
        return self.sess.run(self.simple_model)

# Create two actors.
simulators = [Simulator.remote() for _ in range(2)]

# Run two simulations in parallel.
results = ray.get([s.simulate.remote() for s in simulators])

以下是将TensorFlow与Ray并行的更多示例.

请参见 Ray文档.请注意,我是Ray开发人员之一.

See the Ray documentation. Note that I'm one of the Ray developers.

这篇关于Tensorflow和多处理:通过会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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