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

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

事实上,在强化学习中,有一种算法称为异步优势演员评论家(A3C)每个代理都依赖同一个神经网络来告诉他们在给定状态下应该做什么.换句话说,每个线程并发调用model.predict,就像您遇到的问题一样. 此处.

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() # have to initialize before threading

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函数时才会编译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天全站免登陆