是否可以采用多处理方式在许多训练集上训练keras模型?

时间:2019-01-19 13:03:49

标签: python tensorflow machine-learning keras multiprocessing

我使用conda虚拟环境在PyCharm上运行python v = 3.6.7。我的tensorflow版本是1.12.0。 我想在各种训练集上训练许多keras模型。由于对单个训练集的训练已经相当慢,因此我想使用多处理库的Pool函数运行for循环。 我在下面提供了一个重现此问题的最小示例。

fit_model是我要在其上运行多处理的函数:它以numpy数组作为输入,它是一个训练集,并在其上训练一个简单的keras神经网络(虚拟设置)。 我已经读过,使用tensorflow执行多处理时的一个重要方面是确保我们要进行多处理的函数在顶部具有tensorflow导入,这就是我所做的。 如果我添加以下指令(在“常规”导入下方)

    import tensorflow as tf
    tf.enable_eager_execution()

在fit_model函数的顶部,我显示了错误消息(已截断):

    ValueError: optimizer must be an instance of tf.train.Optimizer, not a <class 'str'>

没有“ enable_eager_execution”指令,我得到以下信息:

    multiprocessing.pool.MaybeEncodingError: Error sending result: 
    '[<tensorflow.python.keras.engine.sequential.Sequential object at 0x00000184FFBAC630>]'. Reason: 'NotImplementedError('numpy() is only available when eager execution is enabled.',)'

请在下面查看我的代码。您可以添加下面提供的一些虚拟训练集:只需将数据拖放到txt文件“ ts1.txt”,“ ts2.txt”,“ ts3.txt”中,然后将其保存在运行代码的位置即可。

训练集1:

    0.041495488 0.297990987 0.724621927 6.129053462
    0.953892097 0.308757758 0.469787044 5.969195238
    0.337488    0.49381137  0.906903059 13.21446917
    0.654812999 0.64815423  0.444301154 9.372944825
    0.119398718 0.152801516 0.59553224  2.786747848
    0.811252373 0.496546    0.899673501 14.13092453
    0.255991654 0.1058905   0.595081192 2.276359761
    0.715703318 0.391053934 0.077776996 2.283026639
    0.404416543 0.348081104 0.439224169 5.08963083
    0.076034482 0.285709566 0.43068089  3.597459163

训练集2:

    0.351528844 0.866463242 0.083273802 2.723360975
    0.689580826 0.875736673 0.783812364 20.59873213
    0.756136259 0.717057395 0.455913086 10.66591631
    0.133306324 0.418013101 0.023300008 0.539324484
    0.948773896 0.253098491 0.455538012 5.125835331
    0.41842445  0.518841476 0.475774703 7.74869507
    0.535329652 0.548263348 0.37085254  6.763755249
    0.665158688 0.501906321 0.031474934 1.772646491
    0.30009976  0.649788342 0.685829413 13.07823033
    0.15021059  0.806372521 0.853050268 19.56095746

训练集3:

    0.990594623 0.229487172 0.28671828  3.823537929
    0.941261097 0.87953271  0.384903849 11.3615169
    0.260536935 0.889700723 0.456944222 11.90429481
    0.543933952 0.683776861 0.269533461 6.248288724
    0.287850026 0.464306966 0.698678531 9.658936707
    0.260487811 0.569814545 0.050767667 1.33096397
    0.836448581 0.642146809 0.350982713 7.98360518
    0.437645129 0.87036494  0.638338969 16.4317503
    0.88084079  0.943387589 0.564804696 16.6809143
    0.859626262 0.453784697 0.951882406 13.81384327

代码:

from multiprocessing import Pool
import numpy

def fit_model(dataset):
        from tensorflow.python.keras.models import Sequential
        from tensorflow.python.keras.layers import Dense
        #import tensorflow as tf
        #tf.enable_eager_execution()
        x = dataset[:, 0:3]
        y = dataset[:, 3]
        model = Sequential()
        model.add(Dense(3, input_dim=3, activation='relu'))
        model.add(Dense(3, activation = 'relu'))
        model.add(Dense(1, activation = 'linear'))
        model.compile(loss='mean_squared_error', optimizer='adam', metrics=['accuracy'])
        model.fit(x, y, epochs=3)
        return model

if __name__ == "__main__":
        dataset1 = numpy.loadtxt('ts1.txt')
        dataset2 = numpy.loadtxt('ts2.txt')
        dataset3 = numpy.loadtxt('ts3.txt')
        datasets = [dataset1, dataset2, dataset3]

        p = Pool()
        models_mp = p.map(fit_model, datasets)
        p.close()
        p.join()

上面的代码不起作用,我开始怀疑我实际上想做什么。

0 个答案:

没有答案
相关问题