简体   繁体   English

在不同线程中进行预测和训练Keras Tensorflow

[英]Predicting and Training in different threads Keras Tensorflow

I am using Keras and Tensorflow to make a kind-of online learning, where I receive new data periodically and I retrain my models with this new data. 我正在使用Keras和Tensorflow进行某种在线学习,我会定期接收新数据,并使用这些新数据重新训练模型。 I can have several models stored in ".h5" files so that when i need to train or predict I load the model and then I perform the necessary operations. 我可以将多个模型存储在“ .h5”文件中,这样当我需要训练或预测时,我会加载模型,然后执行必要的操作。

Currently I separated the training and the predictions in two different threads, so that predictions can be made while the other thread trains. 目前,我将训练和预测分为两个不同的线程,以便可以在其他线程训练时进行预测。 With locks I try to make sure that no prediction or training is done in the same model at the same time (I think this works), but I am aware that keras is not so prepared for this. 对于锁,我尝试确保在同一模型中不会同时进行任何预测或训练(我认为这是可行的),但是我知道keras对此并不准备。 I always some different errors regarding the graph or session of tensorflow, for instance: 我总是在张量流的图形或会话方面遇到一些不同的错误,例如:

Traceback (most recent call last): File "C:\\Users\\a703572\\AppData\\Local\\Programs\\Python\\Python36\\lib\\site-packages\\flask\\app.py", line 2292, in wsgi_app response = self.full_dispatch_request() File "C:\\Users\\a703572\\AppData\\Local\\Programs\\Python\\Python36\\lib\\site-packages\\flask\\app.py", line 1815, in full_dispatch_request rv = self.handle_user_exception(e) File "C:\\Users\\a703572\\AppData\\Local\\Programs\\Python\\Python36\\lib\\site-packages\\flask\\app.py", line 1718, in handle_user_exception reraise(exc_type, exc_value, tb) File "C:\\Users\\a703572\\AppData\\Local\\Programs\\Python\\Python36\\lib\\site-packages\\flask_compat.py", line 35, in reraise raise value File "C:\\Users\\a703572\\AppData\\Local\\Programs\\Python\\Python36\\lib\\site-packages\\flask\\app.py", line 1813, in full_dispatch_request rv = self.dispatch_request() File "C:\\Users\\a703572\\AppData\\Local\\Programs\\Python\\Python36\\lib\\site-packages\\flask\\app.py", line 1799, in dispatch_request return self.view_functionsrule.endpoint File "C:\\Users\\a70357 追溯(最近一次通话):文件“ C:\\ Users \\ a703572 \\ AppData \\ Local \\ Programs \\ Python \\ Python36 \\ lib \\ site-packages \\ flask \\ app.py”,行2292,在wsgi_app response = self.full_dispatch_request中()在full_dispatch_request rv第1815行中的文件“ C:\\ Users \\ a703572 \\ AppData \\ Local \\ Programs \\ Python \\ Python36 \\ lib \\ site-packages \\ flask \\ app.py” rv = self.handle_user_exception(e)文件“ C :\\ Users \\ a703572 \\ AppData \\ Local \\ Programs \\ Python \\ Python36 \\ lib \\ site-packages \\ flask \\ app.py“,第1718行,在handle_user_exception reraise(exc_type,exc_value,tb)中,文件“ C:\\ Users \\ a703572 \\ AppData \\ Local \\ Programs \\ Python \\ Python36 \\ lib \\ site-packages \\ flask_compat.py“,第35行,重新提高价值文件“ C:\\ Users \\ a703572 \\ AppData \\ Local \\ Programs \\ Python \\ Python36 \\ lib \\ site_packages \\ flask \\ app.py“,行1813,在full_dispatch_request中rv = self.dispatch_request()文件” C:\\ Users \\ a703572 \\ AppData \\ Local \\ Programs \\ Python \\ Python36 \\ lib \\ site-packages \\ flask \\ app.py”,行1799,在dispatch_request中返回self.view_functionsrule.endpoint文件“ C:\\ Users \\ a70357 2\\PycharmProjects\\ai-pred-eng\\src\\run_keras_server.py", line 859, in predict_times 0] + '.h5') File "C:\\Users\\a703572\\PycharmProjects\\ai-pred-eng\\src\\run_keras_server.py", line 164, in get_prediction model, scaler = self.load_model_file(self.graph_pred, self.session, path) File "C:\\Users\\a703572\\PycharmProjects\\ai-pred-eng\\src\\run_keras_server.py", line 114, in load_model_file model = load_model(path) File "C:\\Users\\a703572\\AppData\\Local\\Programs\\Python\\Python36\\lib\\site-packages\\keras\\engine\\saving.py", line 419, in load_model model = _deserialize_model(f, custom_objects, compile) File "C:\\Users\\a703572\\AppData\\Local\\Programs\\Python\\Python36\\lib\\site-packages\\keras\\engine\\saving.py", line 287, in _deserialize_model K.batch_set_value(weight_value_tuples) File "C:\\Users\\a703572\\AppData\\Local\\Programs\\Python\\Python36\\lib\\site-packages\\keras\\backend\\tensorflow_backend.py", line 2470, in batch_set_value get_session().run(assign_ops, feed_dict=feed_dict) File "C:\\Users\\a703572\\AppData\\Local 2 \\ PycharmProjects \\ ai-pred-eng \\ src \\ run_keras_server.py“,第859行,在预报时间0] +'.h5')文件“ C:\\ Users \\ a703572 \\ PycharmProjects \\ ai-pred-eng \\ src \\ run_keras_server .py“,第164行,在get_prediction模型中,缩放比例= self.load_model_file(self.graph_pred,self.session,路径)文件“ C:\\ Users \\ a703572 \\ PycharmProjects \\ ai-pred-eng \\ src \\ run_keras_server.py” ,在load_model_file中,第114行,模型= load_model(path)文件,在第419行中,“ C:\\ Users \\ a703572 \\ AppData \\ Local \\ Programs \\ Python \\ Python36 \\ lib \\ site-packages \\ keras \\ engine \\ saving.py” load_model模型= _deserialize_model(f,custom_objects,编译)文件“ C:\\ Users \\ a703572 \\ AppData \\ Local \\ Programs \\ Python \\ Python36 \\ lib \\ site-packages \\ keras \\ engine \\ saving.py”,行287,在_deserialize_model中K.batch_set_value(weight_value_tuples)文件“ C:\\ Users \\ a703572 \\ AppData \\ Local \\ Programs \\ Python \\ Python36 \\ lib \\ site-packages \\ keras \\ backend \\ tensorflow_backend.py”,行2470,位于batch_set_value get_session()中。 (assign_ops,feed_dict = feed_dict)文件“ C:\\ Users \\ a703572 \\ AppData \\ Local \\Programs\\Python\\Python36\\lib\\site-packages\\keras\\backend\\tensorflow_backend.py", line 206, in get_session session.run(tf.variables_initializer(uninitialized_vars)) File "C:\\Users\\a703572\\AppData\\Roaming\\Python\\Python36\\site-packages\\tensorflow\\python\\ops\\variables.py", line 2831, in variables_initializer return control_flow_ops.group(*[v.initializer for v in var_list], name=name) File "C:\\Users\\a703572\\AppData\\Roaming\\Python\\Python36\\site-packages\\tensorflow\\python\\ops\\control_flow_ops.py", line 3432, in group return _GroupControlDeps(dev, deps, name=name) File "C:\\Users\\a703572\\AppData\\Roaming\\Python\\Python36\\site-packages\\tensorflow\\python\\ops\\control_flow_ops.py", line 3384, in _GroupControlDeps return no_op(name=name) File "C:\\Users\\a703572\\AppData\\Local\\Programs\\Python\\Python36\\lib\\contextlib.py", line 88, in exit next(self.gen) File "C:\\Users\\a703572\\AppData\\Roaming\\Python\\Python36\\site-packages\\tensorflow\\python\\framework\\ops.py", line 4249, in device self._device_function_stac \\ Programs \\ Python \\ Python36 \\ lib \\ site-packages \\ keras \\ backend \\ tensorflow_backend.py“,第206行,位于get_session session.run(tf.variables_initializer(uninitialized_vars))文件” C:\\ Users \\ a703572 \\ AppData \\ Roaming \\ Python \\ Python36 \\ site-packages \\ tensorflow \\ python \\ ops \\ variables.py“,第2831行,在variables_initializer中,返回control_flow_ops.group(* [在var_list中,v的v.initializer],名称=名称)文件” C:\\用户\\ a703572 \\ AppData \\ Roaming \\ Python \\ Python36 \\ site-packages \\ tensorflow \\ python \\ ops \\ control_flow_ops.py“,行3432,在组中返回_GroupControlDeps(dev,deps,name = name)文件” C:\\ Users \\ _GroupControlDeps中的a703572 \\ AppData \\ Roaming \\ Python \\ Python36 \\ site-packages \\ tensorflow \\ python \\ ops \\ control_flow_ops.py“行3384返回no_op(name = name)文件” C:\\ Users \\ a703572 \\ AppData \\ Local \\程序\\ Python \\ Python36 \\ lib \\ contextlib.py”,行88,在next(self.gen) 出口中,文件“ C:\\ Users \\ a703572 \\ AppData \\ Roaming \\ Python \\ Python36 \\ site-packages \\ tensorflow \\ python \\ framework \\ ops.py”,第4249行,在设备self._device_function_stac中 k.pop_obj() File "C:\\Users\\a703572\\AppData\\Roaming\\Python\\Python36\\site-packages\\tensorflow\\python\\framework\\traceable_stack.py", line 110, in pop_obj return self._stack.pop().obj IndexError: pop from empty list k.pop_obj()文件“ C:\\ Users \\ a703572 \\ AppData \\ Roaming \\ Python \\ Python36 \\ site-packages \\ tensorflow \\ python \\ framework \\ traceable_stack.py”,行110,在pop_obj中返回self._stack.pop() .obj IndexError:从空列表弹出

Or the error: 或错误:

Exception in thread Thread-1: Traceback (most recent call last): File "C:\\Users\\a703572\\AppData\\Local\\Programs\\Python\\Python36\\lib\\threading.py", line 916, in _bootstrap_inner self.run() File "C:\\Users\\a703572\\AppData\\Local\\Programs\\Python\\Python36\\lib\\threading.py", line 1182, in run self.function(*self.args, **self.kwargs) File "C:\\Users\\a703572\\PycharmProjects\\ai-pred-eng\\src\\run_keras_server.py", line 632, in train self.update_prediction_historics_all() File "C:\\Users\\a703572\\PycharmProjects\\ai-pred-eng\\src\\run_keras_server.py", line 649, in update_prediction_historics_all self.update_prediction_historics_dataset(new_dataset, loadModel=True) File "C:\\Users\\a703572\\PycharmProjects\\ai-pred-eng\\src\\run_keras_server.py", line 672, in update_prediction_historics_dataset 0] + ".h5", loadModel=loadModel)[ File "C:\\Users\\a703572\\PycharmProjects\\ai-pred-eng\\src\\run_keras_server.py", line 198, in get_predictions_sequential model, scaler = self.load_model_file(self.graph_pred, self.session, p 线程Thread-1中的异常:追溯(最近一次调用):_bootstrap_inner self.run()中的文件“ C:\\ Users \\ a703572 \\ AppData \\ Local \\ Programs \\ Python \\ Python36 \\ lib \\ threading.py”,行916。 )运行self.function(* self.args,** self.kwargs)中的文件“ C:\\ Users \\ a703572 \\ AppData \\ Local \\ Programs \\ Python \\ Python36 \\ lib \\ threading.py”,行1182 :\\ Users \\ a703572 \\ PycharmProjects \\ ai-pred-eng \\ src \\ run_keras_server.py“,第632行,位于火车self.update_prediction_historics_all()文件” C:\\ Users \\ a703572 \\ PycharmProjects \\ ai-pred-eng \\ src \\ run_keras_server.py”,第649行,位于update_prediction_historics_all self.update_prediction_historics_dataset(new_dataset,loadModel = True)文件“ C:\\ Users \\ a703572 \\ PycharmProjects \\ ai-pred-eng \\ src \\ run_keras_server_pys_data_preset数据集,672行,更新于] +“ .h5”,loadModel = loadModel)[文件“ C:\\ Users \\ a703572 \\ PycharmProjects \\ ai-pred-eng \\ src \\ run_keras_server.py”,行198,在get_predictions_sequential模型中,scaler = self.load_model_file(self .graph_pred,self.session,p ath) File "C:\\Users\\a703572\\PycharmProjects\\ai-pred-eng\\src\\run_keras_server.py", line 114, in load_model_file model = load_model(path) File "C:\\Users\\a703572\\AppData\\Local\\Programs\\Python\\Python36\\lib\\site-packages\\keras\\engine\\saving.py", line 419, in load_model model = _deserialize_model(f, custom_objects, compile) File "C:\\Users\\a703572\\AppData\\Local\\Programs\\Python\\Python36\\lib\\site-packages\\keras\\engine\\saving.py", line 225, in _deserialize_model model = model_from_config(model_config, custom_objects=custom_objects) File "C:\\Users\\a703572\\AppData\\Local\\Programs\\Python\\Python36\\lib\\site-packages\\keras\\engine\\saving.py", line 458, in model_from_config return deserialize(config, custom_objects=custom_objects) File "C:\\Users\\a703572\\AppData\\Local\\Programs\\Python\\Python36\\lib\\site-packages\\keras\\layers__init__.py", line 55, in deserialize printable_module_name='layer') File "C:\\Users\\a703572\\AppData\\Local\\Programs\\Python\\Python36\\lib\\site-packages\\keras\\utils\\generic_utils.py", line 1 ath)文件“ C:\\ Users \\ a703572 \\ PycharmProjects \\ ai-pred-eng \\ src \\ run_keras_server.py”,行114,位于load_model_file模型= load_model(path)文件“ C:\\ Users \\ a703572 \\ AppData \\ Local \\在load_model模型= _deserialize_model(f,custom_objects,compile)中的Programs \\ Python \\ Python36 \\ lib \\ site-packages \\ keras \\ engine \\ saving.py“行419中,文件“ C:\\ Users \\ a703572 \\ AppData \\ Local \\ Programs \\ Python \\ Python36 \\ lib \\ site-packages \\ keras \\ engine \\ saving.py“,第225行,位于_deserialize_model模型= model_from_config(model_config,custom_objects = custom_objects)文件” C:\\ Users \\ a703572 \\ AppData \\ Local \\ Programs \\ model_from_config中的Python \\ Python36 \\ lib \\ site-packages \\ keras \\ engine \\ saving.py“行458返回反序列化(config,custom_objects = custom_objects)文件” C:\\ Users \\ a703572 \\ AppData \\ Local \\ Programs \\ Python \\ Python36 \\ lib \\ site-packages \\ keras \\ layers__init __。py“,反序列化printable_module_name ='layer'),文件“ C:\\ Users \\ a703572 \\ AppData \\ Local \\ Programs \\ Python \\ Python36 \\ lib \\ site-packages \\ keras \\ utils \\ generic_utils.py”,第1行 45, in deserialize_keras_object list(custom_objects.items()))) File "C:\\Users\\a703572\\AppData\\Local\\Programs\\Python\\Python36\\lib\\site-packages\\keras\\engine\\sequential.py", line 301, in from_config model.add(layer) File "C:\\Users\\a703572\\AppData\\Local\\Programs\\Python\\Python36\\lib\\site-packages\\keras\\engine\\sequential.py", line 181, in add output_tensor = layer(self.outputs[0]) File "C:\\Users\\a703572\\AppData\\Local\\Programs\\Python\\Python36\\lib\\site-packages\\keras\\engine\\base_layer.py", line 431, in call self.build(unpack_singleton(input_shapes)) File "C:\\Users\\a703572\\AppData\\Local\\Programs\\Python\\Python36\\lib\\site-packages\\keras\\layers\\core.py", line 872, in build constraint=self.bias_constraint) File "C:\\Users\\a703572\\AppData\\Local\\Programs\\Python\\Python36\\lib\\site-packages\\keras\\legacy\\interfaces.py", line 91, in wrapper return func(*args, **kwargs) File "C:\\Users\\a703572\\AppData\\Local\\Programs\\Python\\Python36\\lib\\site-packages\\keras\\engine\\base_layer.py", line 252, in add_weight const 45,在deserialize_keras_object列表(custom_objects.items()))中)文件“ C:\\ Users \\ a703572 \\ AppData \\ Local \\ Programs \\ Python \\ Python36 \\ lib \\ site-packages \\ keras \\ engine \\ sequential.py”,行301 ,位于from_config model.add(图层)文件“ C:\\ Users \\ a703572 \\ AppData \\ Local \\ Programs \\ Python \\ Python36 \\ lib \\ site-packages \\ keras \\ engine \\ sequential.py”中,行181,在添加output_tensor = layer(self.outputs [0])在调用 self中的文件“ C:\\ Users \\ a703572 \\ AppData \\ Local \\ Programs \\ Python \\ Python36 \\ lib \\ site-packages \\ keras \\ engine \\ base_layer.py”,行431。 build(unpack_singleton(input_shapes))文件“ C:\\ Users \\ a703572 \\ AppData \\ Local \\ Programs \\ Python \\ Python36 \\ lib \\ site-packages \\ keras \\ layers \\ core.py”,第872行,位于build Constraint = self。 (在bias_constraint中)文件“ C:\\ Users \\ a703572 \\ AppData \\ Local \\ Programs \\ Python \\ Python36 \\ lib \\ site-packages \\ keras \\ legacy \\ interfaces.py”,行91(在包装器中返回func(* args,** kwargs )在add_weight const中的第252行,文件“ C:\\ Users \\ a703572 \\ AppData \\ Local \\ Programs \\ Python \\ Python36 \\ lib \\ site-packages \\ keras \\ engine \\ base_layer.py” raint=constraint) File "C:\\Users\\a703572\\AppData\\Local\\Programs\\Python\\Python36\\lib\\site-packages\\keras\\backend\\tensorflow_backend.py", line 402, in variable v = tf.Variable(value, dtype=tf.as_dtype(dtype), name=name) File "C:\\Users\\a703572\\AppData\\Roaming\\Python\\Python36\\site-packages\\tensorflow\\python\\ops\\variables.py", line 183, in call return cls._variable_v1_call(*args, **kwargs) File "C:\\Users\\a703572\\AppData\\Roaming\\Python\\Python36\\site-packages\\tensorflow\\python\\ops\\variables.py", line 146, in _variable_v1_call aggregation=aggregation) File "C:\\Users\\a703572\\AppData\\Roaming\\Python\\Python36\\site-packages\\tensorflow\\python\\ops\\variables.py", line 125, in previous_getter = lambda **kwargs: default_variable_creator(None, **kwargs) File "C:\\Users\\a703572\\AppData\\Roaming\\Python\\Python36\\site-packages\\tensorflow\\python\\ops\\variable_scope.py", line 2444, in default_variable_creator expected_shape=expected_shape, import_scope=import_scope) File "C:\\Users\\a703572\\AppData\\Roaming\\Python\\P raint = constraint)文件“ C:\\ Users \\ a703572 \\ AppData \\ Local \\ Programs \\ Python \\ Python36 \\ lib \\ site-packages \\ keras \\ backend \\ tensorflow_backend.py”,第402行,变量v = tf.Variable(value ,dtype = tf.as_dtype(dtype),name = name)文件“ C:\\ Users \\ a703572 \\ AppData \\ Roaming \\ Python \\ Python36 \\ site-packages \\ tensorflow \\ python \\ ops \\ variables.py”,第183行,在调用返回cls._variable_v1_call(* args,** kwargs)文件“ C:\\ Users \\ a703572 \\ AppData \\ Roaming \\ Python \\ Python36 \\ site-packages \\ tensorflow \\ python \\ ops \\ variables.py”,行146,在_variable_v1_call中聚合=聚合)文件“ C:\\ Users \\ a703572 \\ AppData \\ Roaming \\ Python \\ Python36 \\ site-packages \\ tensorflow \\ python \\ ops \\ variables.py”,第125行,位于previous_getter = lambda ** kwargs:default_variable_creator(无,** kwargs)文件“ C:\\ Users \\ a703572 \\ AppData \\ Roaming \\ Python \\ Python36 \\ site-packages \\ tensorflow \\ python \\ ops \\ variable_scope.py”,第2444行,位于default_variable_creator Expected_shape = expected_shape,import_scope = import_scope)文件“ C:\\ Users \\ a703572 \\ AppData \\ Roaming \\ Python \\ P ython36\\site-packages\\tensorflow\\python\\ops\\variables.py", line 187, in call return super(VariableMetaclass, cls). ython36 \\ site-packages \\ tensorflow \\ python \\ ops \\ variables.py“,行187,在调用返回super(VariableMetaclass,cls)中。 call (*args, **kwargs) File "C:\\Users\\a703572\\AppData\\Roaming\\Python\\Python36\\site-packages\\tensorflow\\python\\ops\\variables.py", line 1329, in init constraint=constraint) File "C:\\Users\\a703572\\AppData\\Roaming\\Python\\Python36\\site-packages\\tensorflow\\python\\ops\\variables.py", line 1492, in _init_from_args ops.add_to_collections(collections, self) File "C:\\Users\\a703572\\AppData\\Local\\Programs\\Python\\Python36\\lib\\contextlib.py", line 88, in exit next(self.gen) File "C:\\Users\\a703572\\AppData\\Roaming\\Python\\Python36\\site-packages\\tensorflow\\python\\framework\\ops.py", line 5347, in init_scope yield File "C:\\Users\\a703572\\AppData\\Roaming\\Python\\Python36\\site-packages\\tensorflow\\python\\framework\\ops.py", line 4369, in exit self._graph._pop_control_dependencies_controller(self) File "C:\\Users\\a703572\\AppData\\Roaming\\Python\\Python36\\site-packages\\tensorflow\\python\\framework\\ops.py", line 4390, in _pop_control_dependencies_controller assert self._control_dependencies_stack[-1] is controller Asser 调用 (* args,** kwargs)文件“ C:\\ Users \\ a703572 \\ AppData \\ Roaming \\ Python \\ Python36 \\ site-packages \\ tensorflow \\ python \\ ops \\ variables.py”,行1329, init约束=约束)文件“ C:\\ Users \\ a703572 \\ AppData \\ Roaming \\ Python \\ Python36 \\ site-packages \\ tensorflow \\ python \\ ops \\ variables.py”第1492行,位于_init_from_args ops.add_to_collections(collections,self)中文件“ C:\\用户\\ a703572 \\ AppData \\ Local \\ Programs \\ Python \\ Python36 \\ lib \\ contextlib.py”,行88,在出口 next(self.gen)文件“ C:\\ Users \\ a703572 \\ AppData \\ Roaming \\ Python \\ Python36 \\ site -packages \\ tensorflow \\ python \\ framework \\ ops.py“,行5347,在init_scope中产生文件“ C:\\ Users \\ a703572 \\ AppData \\ Roaming \\ Python \\ Python36 \\ site-packages \\ tensorflow \\ python \\ framework \\ ops.py ”,行4369,在退出 self._graph._pop_control_dependencies_controller(self)文件中,“ C:\\ Users \\ a703572 \\ AppData \\ Roaming \\ Python \\ Python36 \\ site-packages \\ tensorflow \\ python \\ framework \\ ops.py”,行4390, _pop_control_dependencies_controller中的assert self._control_dependencies_stack [-1]是控制器Asser tionError tionError

My solution was using a graph for prediction and a graph for training, and every time I want to perform a tf operation I use: 我的解决方案是使用图形进行预测,并使用图形进行训练,每次我想执行tf操作时,我都会使用:

with server_predict.graph_pred.as_default():
    with tf.Session(graph=server_predict.graph_pred) as sess:

And I also added the line: 我还添加了这一行:

        backend.set_session(sess)

Despite this, I keep having the errors coming from the tf session or graph, as It seems that the operations are not properly separated. 尽管如此,由于操作似乎没有正确分开,所以我仍然会出现来自tf会话或图形的错误。 Another error is the one I wrote in this issue that is still opened, regarding the tf session. 另一个错误是我在本期中仍未解决的关于tf会话的错误。 Solution given using k.clear_session() (k = keras backend) did not work for me. 使用k.clear_session()(k = keras后端)给出的解决方案对我不起作用。

Does any one have had a similar problem or has programmed a similar task that might help me? 是否有人遇到过类似的问题或编写了可能对我有帮助的类似任务?

Thanks!! 谢谢!!


Found a "wrap" to make this work. 找到一个“包装纸”使这项工作。 Instead of launching two threads over the same class (custom), what I have is two objects of the same class, one is dedicated to training and the other to predict. 我没有在同一个类(自定义)上启动两个线程,而是拥有两个相同类的对象,一个对象专用于训练,另一个对象用于预测。 This is not a real multithread app (even though the two objects are launched from the same main). 这不是真正的多线程应用程序(即使两个对象是从同一主程序启动的)。 Until I (we) find a proper multithread solution this might help. 在找到合适的多线程解决方案之前,这可能会有所帮助。

However I do not understand know how I got the errors before, and just by having two objects not, even if these objects run in the same process. 但是,我不知道我以前是怎么得到错误的,并且仅仅知道没有两个对象,即使这些对象在同一进程中运行也是如此。 Is it that keras/tensorflow can only make operations on only one graph but defines different graphs for different objects on the same process? keras / tensorflow只能在一个图上进行操作,而在同一进程中为不同对象定义不同的图吗?

Easiest solution is to have two separate keras models - the first runs in inference mode, and the second runs in training mode. 最简单的解决方案是拥有两个单独的keras模型-第一个在推理模式下运行,第二个在训练模式下运行。 Every time the inference model gets a new dataset to predict on, it first checks to see if it has the most "up to date" .h5 file, if not then it loads it in first then runs the prediction. 每次推理模型获取新的数据集进行预测时,首先会检查其是否具有最新的.h5文件,如果没有,则首先将其加载,然后运行预测。 This way you can avoid locks and such. 这样您可以避免锁等。

It's hard to give advice specific to your case because what you want is likely not the same as what I need 很难针对您的情况提供建议,因为您想要的可能与我需要的不一样

  • This is my opinion after having done something similar with Tensorflow Multiprocessing 这是我在使用Tensorflow Multiprocessing完成类似操作后的看法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM