我正在尝试keras使用多个线程(和tensorflow后端)训练具有不同参数值的多个模型.我已经看到了在多个线程中使用相同模型的一些示例,但在这种特殊情况下,我遇到了有关冲突图等的各种错误.这是我希望能够做的一个简单示例:
from concurrent.futures import ThreadPoolExecutor
import numpy as np
import tensorflow as tf
from keras import backend as K
from keras.layers import Dense
from keras.models import Sequential
sess = tf.Session()
def example_model(size):
model = Sequential()
model.add(Dense(size, input_shape=(5,)))
model.add(Dense(1))
model.compile(optimizer='sgd', loss='mse')
return model
if __name__ == '__main__':
K.set_session(sess)
X = np.random.random((10, 5))
y = np.random.random((10, 1))
models = [example_model(i) for i in range(5, 10)]
e = ThreadPoolExecutor(4)
res_list = [e.submit(model.fit, X, y) for model in models]
for …Run Code Online (Sandbox Code Playgroud) concurrency multithreading python-multithreading keras tensorflow