代码之家  ›  专栏  ›  技术社区  ›  Giordano Prashanth Babu

运行keras multi_gpu_模型时的预测误差

  •  0
  • Giordano Prashanth Babu  · 技术社区  · 6 年前

    我在谷歌云平台实例上运行Keras模型时遇到了一个问题。
    模型如下:

    n_timesteps, n_features, n_outputs = train_x.shape[1], train_x.shape[2], train_y.shape[1]
    
    train_y = train_y.reshape((train_y.shape[0], train_y.shape[1], 1))
    
    verbose, epochs, batch_size = 1, 1, 64  # low number of epochs just for testing purpose
    with tf.device('/cpu:0'):
        m = Sequential()
        m.add(CuDNNLSTM(20, input_shape=(n_timesteps, n_features)))
        m.add(LeakyReLU(alpha=0.1))
        m.add(RepeatVector(n_outputs))
        m.add(CuDNNLSTM(20, return_sequences=True))
        m.add(LeakyReLU(alpha=0.1))
        m.add(TimeDistributed(Dense(20)))
        m.add(LeakyReLU(alpha=0.1))
        m.add(TimeDistributed(Dense(1)))
    
    self.model = multi_gpu_model(m, gpus=8)
    self.model.compile(loss='mse', optimizer='adam')
    
    self.model.fit(train_x, train_y, epochs=epochs, batch_size=batch_size, verbose=verbose)
    

    正如您从上面的代码中看到的,我在带有8个GPU(Nvidia Tesla K80)的机器上运行该模型。
    火车运行良好,没有任何错误。但是,预测失败并返回以下错误:

    W tensorflow/core/framework/op_kernel。抄送:1502]操作要求在cudnn_rnn_操作失败。抄送:1336:未知:CUDNN_状态_坏_参数 在tensorflow/stream_executor/cuda/cuda_dnn中。抄送(1285):“cudnsetensornddescriptor(tensor_desc.get(),数据类型,sizeof(dims)/sizeof(dims[0]),dims,Steps”

    下面是运行预测的代码:

    self.model.predict(input_x)
    

    我注意到,如果我删除了多GPU数据并行的代码,那么代码在使用单个GPU时运行良好。
    更准确地说,如果我对这一行进行注释,代码可以正常工作

    self.model = multi_gpu_model(m, gpus=8)
    

    我错过了什么?

    虚拟信息

    cudatoolkit-10.0.130
    cudnn-7.6.4
    keras-2.2.4
    keras应用——1.0.8
    凯拉斯基地-2.2.4
    keras gpu-2.2.4
    python-3.6

    使现代化

    train_x.shape = (1441, 288, 1)
    train_y.shape = (1441, 288, 1)
    input_x.shape = (1, 288, 1)
    

    在奥利维尔·德哈恩的回答之后,我尝试了他的建议,结果奏效了。
    我试图修改输入_x形状以获得(8288,1)。
    为了做到这一点,我还修改了train_x和train_y形状。
    这里是一个总结:

    train_x.shape = (8065, 288, 1)
    train_y.shape = (8065, 288, 1)
    input_x.shape = (8, 288, 1)
    

    但现在我在训练阶段也犯了同样的错误,在这一行:

    self.model.fit(train_x, train_y, epochs=epochs, batch_size=batch_size, verbose=verbose)
    
    0 回复  |  直到 6 年前
        1
  •  3
  •   Olivier Dehaene    6 年前

    tf.keras.utils.multi_gpu_model 我们可以看到它的工作原理如下:

    • 将模型的输入分成多个子批次。
    • 在每个子批次上应用模型副本。每个模型副本都在专用GPU上执行。
    • 将结果(在CPU上)连接成一个大批量。

    您正在触发一个错误,因为 CuDNNLSTM 至少一个模型副本的图层为空。这是因为divide操作需要: input // n_gpus > 0

    请尝试以下代码:

    input_x = np.random.randn(8, n_timesteps, n_features)
    model.predict(input_x)