我试图在一个GPU上并行安装多个小型Keras型号 . 由于我需要将它们从列表中删除并一次训练它们的原因 . 由于我不熟悉标准的多处理模块,我使用了pathos .
我试图做的是这样的:
from pathos.multiprocessing import ProcessPool as Pool
import tensorflow as tf
import keras.backend as K
def multiprocess_step(self, model):
K.set_session(sess)
with sess.graph.as_default():
model = step(model, sess)
return model
def step(model, sess):
K.set_session(sess)
with sess.graph.as_default():
model.fit(x=data['X_train'], y=data['y_train'],
batch_size=batch_size
validation_data=(data['X_test'], data['y_test']),
verbose=verbose,
shuffle=True,
initial_epoch=self.step_num - 1)
return model
config = tf.ConfigProto()
config.gpu_options.allow_growth = True
config.gpu_options.visible_device_list = "0"
sess = tf.Session(config=config)
K.set_session(sess)
with sess.graph.as_default():
pool = Pool(8).map
model_list = pool(multiprocess_step, model_list)
但无论我尝试什么,我都会收到一个错误声称模型似乎不在同一个图表上......
ValueError: Tensor("training/RMSprop/Variable:0", shape=(25, 352), dtype=float32_ref) must be from the same graph as Tensor("RMSprop/rho/read:0", shape=(), dtype=float32).
异常起源于model.fit()行,所以即使我试图在每个可能的位置设置它,我必须对会话图的分配做错了吗?
有没有人有类似的经历?
2 回答
Keras issue tracker建议如下 . 与使用多处理相比,我不确定该方法的相对优点 .
考虑到后端设置为keras的tensorflow . 您可以使用代码并对多个模型调用/多个模型加载执行并行处理 .
并且对于特定模型的调用,进行以下实验 . 对于模型1预测执行以下操作
对于模型2,它遵循相同的