如何并行训练多个keras模型?

时间:2019-07-04 09:43:24

标签: python for-loop tensorflow keras parallel-processing

我有100个不同的数据集。我想训练一个keras模型100次,并且每次都使用一个数据集。这是我的代码

from keras.layers import Input, Dense
from keras.models import Model
from time import time
import numpy as np

time_begin = time()

features = {}
labels = {}
In = {}
dense_1 = {}
dense_1_output = {}
dense_2 = {}
dense_2_output = {}
model = {}

num_model = 100
num_data = 1000
input_dim = 5
for i in range(num_model):
    # generate 100 different datasets, the i-th dataset is features[i], labels[i]
    features[i] = np.random.normal(0, 1, (num_data, input_dim))
    labels[i] = np.sum(features[i], axis=1)

    In[i] = Input((input_dim,), name='input_'+str(i))
    dense_1[i] = Dense(units=20, activation='sigmoid', name='dense_1_'+str(i))
    dense_1_output[i] = dense_1[i](In[i])
    dense_2[i] = Dense(units=1, activation='linear', name='dense_2_'+str(i))
    dense_2_output[i] = dense_2[i](dense_1_output[i])

    # build and train 100 models over 100 different datasets
    model[i] = Model(In[i], dense_2_output[i])
    model[i].compile(optimizer='adam', loss='mean_squared_error')
    model[i].fit(features[i], 
                 labels[i], 
                 batch_size=64, 
                 epochs=5, 
                 verbose=2,
                 validation_split=.2)

print('The code runs {} minutes.'.format((time()-time_begin)/60.0))

for循环for i in range(num_model)将有序执行。谁能教我如何改善这一点并同时进行培训?

0 个答案:

没有答案