Keras 线程安全吗?

编程入门 行业动态 更新时间:2024-10-24 16:25:07
本文介绍了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 线程安全吗?

本文发布于:2023-11-25 18:58:53,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1630952.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:线程   Keras

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!