如何使用keras正确实施多处理?

时间:2018-07-11 09:13:34

标签: python tensorflow keras multiprocessing

我正在尝试扩展我的项目以充分利用我的CPU,但是我遇到了使用keras和正确进行多处理的难题。我的代码正在尝试并行模拟多个游戏。

def create_model(): #Very simple conv-net
    import keras
    from keras.models import Model
    from keras.layers import Input, Add, Dense, Flatten, Conv2D, Activation
    from keras.models import model_from_json
    from keras.initializers import glorot_uniform

    inputs =  Input(shape=(8,8,1))
    X = Conv2D(filters=32, kernel_size=(3,3), strides=(1,1), kernel_initializer=glorot_uniform())(inputs)
    X = Activation('relu')(X)
    X = Flatten()(X)
    X = Dense(10, activation='relu', kernel_initializer=glorot_uniform())(X)
    output = Dense(1, activation='sigmoid', name='output', kernel_initializer=glorot_uniform())(X)

    model = Model(inputs=inputs, outputs=output)

    model.compile(optimizer='adam', loss={'output': 'binary_crossentropy'})

    return model

def convert(board):
    import numpy as np
    print("converting")


    string_board = str(board)
    data_board = string_board.split()
    final_data = []

    #normalized board values
    variance = 14
    convert = {'.': 0.0, 'p': -1.0, 'r': -2.0, 'n': -3.0, 'b': -4.0, 'q': -5.0, 'k': -6.0,
     'P': 1.0, 'R': 2.0, 'N': 3.0, 'B': 4.0, 'Q': 5.0, 'K': 6.0}
    for char in data_board:
        final_data.append(convert[char])

    final_data = np.asarray(final_data)
    final_data /= variance
    if not board.turn:
        final_data = -final_data
    final_data = final_data.reshape(1, 8, 8, 1)
    return final_data


return_list = []
model = create_model() #This just creates a conv-net



def board_prediction(board): #Get predictions in parallel 
    print("predicting")

    position = convert(board) #Prep board for CNN
    probs, _ = model.predict(position)

    return probs


def log_result(result):
    return_list.append(results)

def apply_async():
    import chess
    from multiprocessing import Manager, Process, Pool

    pool = Pool()
    for i in range(1):
        board = chess.Board()
        pool.apply_async(board_prediction, args=(board, ), callback=log_result)
    pool.close()
    pool.join()

    print(return_list)


if __name__ == "__main__": #For future multiprocessing
    #Remove tensorflow intialization message for a cleaner cmd
    import os
    import tensorflow as tf
    os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
    tf.logging.set_verbosity(tf.logging.ERROR)

    with tf.Session(config=tf.ConfigProto(device_count={'GPU': 0})) as sess:
        pass

    apply_async()

我的代码打印我的两条打印行,但输出一个空列表。同样,将for循环的次数扩展到3会从我的计算机(带有64GB RAM)中吸取几乎所有的内存。

其他信息: 我使用的是单个GTX 1080 GPU和16核threadripper CPU

谢谢

0 个答案:

没有答案