Keras 线程安全吗? [英] Is Keras thread safe?

查看:36
本文介绍了Keras 线程安全吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Python 和 Keras(目前使用 Theano 后端,但我对切换没有任何疑虑).我有一个神经网络,我可以并行加载和处理多个信息源.目前,我一直在一个单独的进程中运行每一个,它从文件中加载自己的网络副本.这似乎是对 RAM 的浪费,因此我认为将单个多线程进程与所有线程使用的一个网络实例一起使用会更有效.但是,我想知道 Keras 是否对任一后端都是线程安全的.如果我在不同的线程中同时在两个不同的输入上运行 .predict(x),我会遇到竞争条件或其他问题吗?

I'm using Python and Keras (currently using Theano backend, but I have no qualms with switching). I have a neural network that I load and process multiple sources of information with in parallel. Currently, I've been running each one in a separate process and it loads its own copy of the network from the file. This seems like a waste of RAM, so I was thinking it would be more efficient to have a single multi-threaded process with one instance of the network that is used by all threads. However, I'm wondering if Keras is thread safe with either backend. If I run .predict(x) on two different inputs at the same time in different threads, will I run into race conditions or other issues?

谢谢

推荐答案

是的,Keras 是线程安全的,如果你稍微注意一下的话.

Yes, Keras is thread safe, if you pay a little attention to it.

事实上,在强化学习中有一种算法叫做Asynchronous Advantage Actor Critics (A3C)每个代理都依赖同一个神经网络来告诉他们在给定状态下应该做什么.换句话说,每个线程在您的问题中同时调用 model.predict .此处是使用 Keras 的示例实现.

In fact, in reinforcement learning there is an algorithm called Asynchronous Advantage Actor Critics (A3C) where each agent relies on the same neural network to tell them what they should do in a given state. In other words, each thread calls model.predict concurrently as in your problem. An example implementation with Keras of it is here.

但是,如果您查看代码,则应该特别注意这一行:model._make_predict_function() # 线程前必须初始化

You should, however, pay extra attention to this line if you looked into the code: model._make_predict_function() # have to initialize before threading

Keras 文档中从未提到过这一点,但有必要使其同时工作.简而言之,_make_predict_function是一个编译predict函数的函数.在多线程设置中,你必须手动调用这个函数来提前编译predict,否则predict函数直到你第一次运行它才会被编译,这将当许多线程同时调用它时会出现问题.您可以在此处查看详细说明.

This is never mentioned in the Keras docs, but its necessary to make it work concurrently. In short, _make_predict_function is a function that compiles the predict function. In multi thread setting, you have to manually call this function to compile predict in advance, otherwise the predict function will not be compiled until you run it the first time, which will be problematic when many threading calling it at once. You can see a detailed explanation here.

到目前为止,我还没有遇到过 Keras 中多线程的任何其他问题.

I have not met any other issues with multi threading in Keras till now.

这篇关于Keras 线程安全吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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