如何正确使用并发期货来运行model.fit?

时间:2019-04-23 03:23:14

标签: python-3.x multithreading tensorflow keras concurrent.futures

我有一个像这样的python脚本:

import tensorflow as tf
from tensorflow import keras
import numpy  as np
import pandas as pd
import argparse
from concurrent import futures

[data ETL processing]


#Model code 
model = keras.Sequential()

input_layer = keras.layers.Dense(20, input_shape=[20], activation='tanh')
model.add(input_layer)

output_layer = keras.layers.Dense(1, activation='sigmoid')
model.add(output_layer)

gd = tf.train.GradientDescentOptimizer(0.01)

model.compile(optimizer=gd, loss='mse')

sess = tf.Session()  #NEW LINE


training_y = dependent_variables #produced in the data ETL processing section above
training_x = independent_variable#produced in the data ETL processing section above



init_op = tf.initializers.global_variables()



sess.run(init_op)#NEW LINE

def model_fit():

    model.fit(training_x, training_y, epochs=20, steps_per_epoch =20 )
    return model




with futures.ThreadPoolExecutor() as executor:# Or use ProcessPoolExecutor
        executor.map(model_fit())

我正确地使用了多重处理来运行model_fit函数吗?

在运行多线程库和将'model.fit'作为行本身运行之间,我看不到任何改进。

0 个答案:

没有答案