gpt4 book ai didi

python - 运行 keras multi_gpu_model 时预测出错

转载 作者:行者123 更新时间:2023-12-02 03:31:40 27 4
gpt4 key购买 nike

我在 Google Cloud Platform 实例上运行 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.cc:1502] OP_REQUIRES failed at cudnn_rnn_ops.cc:1336 : Unknown: CUDNN_STATUS_BAD_PARAM in tensorflow/stream_executor/cuda/cuda_dnn.cc(1285): 'cudnnSetTensorNdDescriptor( tensor_desc.get(), data_type, sizeof(dims) / sizeof(dims[0]), dims, strides)'

这里是运行预测的代码:

self.model.predict(input_x)

我注意到,如果我删除多 GPU 数据并行的代码,该代码使用单个 GPU 就能很好地工作。
更准确地说,如果我注释掉这一行,代码就会正常工作

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

我错过了什么?

virtualenv信息

cudatoolkit - 10.0.130
cudnn - 7.6.4
keras - 2.2.4
keras-applications - 1.0.8
keras-base - 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)

在 Olivier Dehaene 回复后,我尝试了他的建议,结果成功了。
我尝试修改 input_x 形状以获得 (8, 288, 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)

最佳答案

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

  • Divide the model's input(s) into multiple sub-batches.
  • Apply a model copy on each sub-batch. Every model copy is executed on a dedicated GPU.
  • Concatenate the results (on CPU) into one big batch.

您正在触发错误,因为至少一个模型副本的 CuDNNLSTM 图层的输入为空。这是因为除法运算需要:input//n_gpus > 0

试试这个代码:

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

关于python - 运行 keras multi_gpu_model 时预测出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58846318/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com