如何并行运行几个Keras神经网络

时间:2016-07-22 12:42:43

标签: theano python-multiprocessing keras

我正在尝试使用Keras来运行强化学习算法。在这个算法中,我正在训练一个神经网络。与其他学习问题的不同之处在于,我需要使用神经网络本身来生成训练数据,并在更新后重复此操作。当我试图并行生成训练数据时,我遇到了问题。

问题在于我不能告诉Theano在训练时使用GPU,因为它在生成训练数据时也会使用GPU,并且如果多个进程调用会导致问题。

更重要的是,即使我在THEANO_FLAGS='floatX=float32,device=cpu,openmp=True' OMP_NUM_THREADS=4命令之前写python,我也不会在多线程模式下运行。这不会导致任何错误,但我可以看到只有一个线程正在运行。

这是我的代码。它是一个简化版本。

import numpy
from numpy import array
import copy
from time import time
import multiprocessing

from keras.models import Sequential
from keras.layers import Dense, Activation
from keras.optimizers import SGD
from keras.models import model_from_json

def runEpisode(qn):
    # Some codes that need qn.predict
    result = qn.predict(array([[1, 3]])) # That's just for demo

    return ([1, 2], 2) # Generated some training data, (X, Y)

def runMultiEpisode(qn, queue, event, nEpisode): # 'queue' is used to return result. 'event' is set when terminates.
    # Run several Episodes
    result = []

    for i in range(nEpisode):
        result.append(runEpisode(qn))

    # Return the result to main process
    queue.put(result)
    event.set()

def runEpisode_MultiProcess(nThread, qn, nEpisode):
    processes = []
    queues = []
    events = []

    rewardCount = 0.0

    # Start processes
    for i in range(nThread):
        queue = multiprocessing.Queue()
        event = multiprocessing.Event()
        p = multiprocessing.Process(target = runMultiEpisode, 
            args = (qn, queue, event, int(nEpisode/nThread)))
        p.start()

        processes.append(p)
        queues.append(queue)
        events.append(event)

    # Wait for result
    for event in events:
        event.wait()

    # Gather results
    result = []

    for queue in queues:
        result += queue.get()

    print 'Got', len(result), 'samples'

    return result

def train(qn, nEpisode):
    newqn = copy.copy(qn)

    # Generate training data
    print 'Running episodes'
    t = time()
    result = runEpisode_MultiProcess(2, qn, nEpisode)
    print 'Time:', time() - t

    # Fit the neural network
    print 'Begin fitting'
    t = time()
    newqn.fit([x[0] for x in result], [x[1] for x in result])

    return newqn

qn = Sequential([Dense(2, input_dim = 2, activation = 'sigmoid'),
    Dense(1, activation = 'linear'),])
qn.compile(optimizer =
    SGD(lr = 0.001,
        momentum = 0.9),
    loss = 'mse',
    metrics = [])

train(qn, 100)

所以我有两个问题:

  1. 我可以告诉Theano仅在拟合数据时使用GPU吗?
  2. 什么可能导致Theano不使用多线程?
  3. 编辑:我发现Theano将在我自己的机器上使用多线程,但不会在远程服务器上使用。我是在徘徊,这是否是由一些错误的配置引起的。

0 个答案:

没有答案