Tensorflow 和多处理:传递会话 [英] Tensorflow and Multiprocessing: Passing Sessions

查看:43
本文介绍了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).

我建议使用 actors 并行化代码,它们本质上是并行计算的模拟对象"并用于在分布式设置中管理状态.

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天全站免登陆