Multiprocessing with Keras

Viewed 1778

I'm stuck with the following error while I'm trying to parallelize training process over multiple cores. Any help, please? or is there any other way to do it?

from sklearn import datasets
X, y = datasets.make_classification(n_samples=1000, n_features=20,
                                    n_informative=2, n_redundant=10,
                                    random_state=42)

def train(x):
    import tensorflow as tf
    from keras.models import Sequential
    from keras.layers import Dense, Activation
    from keras import backend as K


    config = tf.ConfigProto(intra_op_parallelism_threads=1,
                            inter_op_parallelism_threads=1, 
                            allow_soft_placement=True)    
    session = tf.Session(config=config)
    K.set_session(session)


    model = Sequential()
    model.add(Dense(units=64, input_dim=20))
    model.add(Activation('relu'))
    model.add(Dense(units=1))
    model.add(Activation('softmax'))
    model.compile(optimizer='adam', 
                  loss='binary_crossentropy', 
                  metrics=['accuracy'])
    model.fit(X, y, epochs=5, batch_size=32)
    K.clear_session()
    return x


import multiprocessing
ls = [1,2,3]
pool = multiprocessing.Pool()
results = pool.map(train, ls)
pool.close()
pool.terminate()

Using TensorFlow backend. Using TensorFlow backend. Using TensorFlow backend. Epoch 1/5 Exception in thread Thread-9: Traceback (most recent call last): File "/usr/lib/python2.7/threading.py", line 801, in __bootstrap_inner self.run() File "/usr/lib/python2.7/threading.py", line 754, in run self.__target(*self.__args, **self.__kwargs) File "/usr/lib/python2.7/multiprocessing/pool.py", line 389, in _handle_results task = get() TypeError: ('init() takes exactly 4 arguments (1 given)', , ())

0 Answers
Related