相关疑难解决方法(0)

同时运行多个tensorflow会话

我试图在具有64个CPU的CentOS 7机器上同时运行几个TensorFlow会话.我的同事报告说他可以使用以下两个代码块在他的机器上使用4个内核生成并行加速:

mnist.py

import numpy as np
import input_data
from PIL import Image
import tensorflow as tf
import time


def main(randint):
    print 'Set new seed:', randint
    np.random.seed(randint)
    tf.set_random_seed(randint)
    mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)

    # Setting up the softmax architecture
    x = tf.placeholder("float", [None, 784])
    W = tf.Variable(tf.zeros([784, 10]))
    b = tf.Variable(tf.zeros([10]))
    y = tf.nn.softmax(tf.matmul(x, W) + b)

    # Setting up the cost function
    y_ = tf.placeholder("float", [None, 10])
    cross_entropy = -tf.reduce_sum(y_*tf.log(y))
    train_step = tf.train.GradientDescentOptimizer(0.01).minimize(cross_entropy)

    # Initialization 
    init = tf.initialize_all_variables()
    sess = …
Run Code Online (Sandbox Code Playgroud)

python parallel-processing python-multiprocessing tensorflow

15
推荐指数
1
解决办法
1万
查看次数

Tensorflow服务器:我不想为每个会话初始化全局变量

EDIT2:下面的Github链接包含从进程调用TF模型的问题的可能解决方案.它们包括急切执行和专用服务器进程,通过http请求提供TF模型预测.我想知道是否使用自定义服务器和请求我赢得任何时间相比每次初始化全局变量和调用tf.train.Server,但它似乎更优雅的方式.

我将调查内存泄漏,如果它消失了,请关闭此问题.

编辑:添加简单的可重现的问题示例:

https://github.com/hcl14/Tensorflow-server-launched-from-child-process


背景:我正在运行Tensorflow服务器,并从"分叉"进程连接到它.动态创建(和销毁)进程对我来说至关重要 - 我在那里移动了高负载的代码部分,因为奇怪的内存泄漏,Python剖析器看不到(线程无法解决问题).因此,我希望快速初始化流程并立即开始工作.仅当进程被销毁时才释放内存.

做实验,我发现了一个解决方案,当加载的模型和图形被保存到全局变量中,然后由子进程(默认使用'fork'模式)进行,然后调用服务器.

问题:对我来说奇怪的是,在加载keras模型之后,我无法锁定我不希望修改的图形,并且tf.global_variables_initializer()每次在子进程中打开新会话时我都需要运行.但是,在没有任何会话创建的主流程中运行虚拟运行Ok.我知道在这种情况下,tensorflow使用默认会话,但是图形上的所有变量都应该在模型运行后初始化,所以我希望新会话能够使用先前定义的图形工作.

因此,我认为修改模型会使Python对子进程('fork'模式)大量腌制,从而产生计算和内存开销.

请原谅我的很多代码.我使用的模型是遗留和黑盒子,所以我的问题可能与它有关.Tensorflow版本是1.2(我无法升级,模型不兼容),Python 3.6.5.

此外,也许我的解决方案效率低下而且效果更好,我将非常感谢您的建议.

我的设置如下:

1.Tensorflow服务器在主进程中启动:

初始化服务器:

def start_tf_server():
    import tensorflow as tf
    cluster = tf.train.ClusterSpec({"local": [tf_server_address]})
    server = tf.train.Server(cluster, job_name="local", task_index=0)    
    server.join() # block process from exiting
Run Code Online (Sandbox Code Playgroud)

在主要过程中:

p = multiprocessing.Process(target=start_tf_server)
p.daemon=True
p.start() # this process never ends, unless tf server crashes

# WARNING! Graph initialization must be made only after Tf server start!
# Otherwise everything will …
Run Code Online (Sandbox Code Playgroud)

python tensorflow

15
推荐指数
1
解决办法
811
查看次数

Tensorflow:将会话传递给python多进程

我正在使用tensorflow来预处理一些大图像.我遇到了一个内存迅速崩溃的问题.我转向在python中使用多处理,因此内存将在我想要的时候完全释放.

问题是,我正在使用python的多进程队列,由于某种原因未知,我无法将我的tensorflow会话从我的父进程传递给子进程.使用一些高级调试技术(即每几行打印一些东西)我注意到python在我使用会话的行内空闲,它不会抛出错误消息.

我的代码看起来像这样:

def subprocess(some_image, sess, q):
    with sess.as_default():
        # ... use sess and q ...
        print "All good and well" #This is printed
        some_image.eval() #Nothing happens here in console
        print "Still all good and well" #This is not printed

if __name__ == '__main__':
    # ... some initial operations ...
    some_image = read_some_image()

    sess = tf.Session()

    q = Queue()
    q.put(something)
    p = Process(target=subprocess, args=(some_image, sess, q))
    p.start()
    p.join()
Run Code Online (Sandbox Code Playgroud)

可能是什么问题呢?非常感谢!

python python-2.7 python-multiprocessing tensorflow

9
推荐指数
1
解决办法
5264
查看次数