简体   繁体   English

带有 CuDNNLSTM 层的 Keras 模型在生产服务器上不起作用

[英]Keras Model With CuDNNLSTM Layers Doesn't Work on Production Server

I have used an AWS p3 instance to train the following model using GPU acceleration:我使用 AWS p3 实例使用 GPU 加速训练以下模型:

x = CuDNNLSTM(128, return_sequences=True)(inputs)
x = Dropout(0.2)(x)
x = CuDNNLSTM(128, return_sequences=False)(x)
x = Dropout(0.2)(x)
predictions = Dense(1, activation='tanh')(x)
model = Model(inputs=inputs, outputs=predictions)

After training I saved the model with Keras' save_model function and moved it to a separate production server that doesn't have a GPU.训练后,我使用save_model函数保存了模型,并将其移至没有 GPU 的单独生产服务器。

When I attempt to predict using the model on the production server it fails with the following error:当我尝试在生产服务器上使用该模型进行预测时,它失败并显示以下错误:

No OpKernel was registered to support Op 'CudnnRNN' with these attrs.没有注册任何 OpKernel 来支持具有这些属性的 Op 'CudnnRNN'。 Registered devices: [CPU], Registered kernels:注册设备:[CPU],注册内核:

I'm guessing this is because the production server doesn't have GPU support, but I was hoping this wouldn't be a problem.我猜这是因为生产服务器没有 GPU 支持,但我希望这不会成为问题。 Is there any way I can use this model on a production server without a GPU?有什么办法可以在没有 GPU 的生产服务器上使用这个模型?

No, you can't, CuDNN requires the use of a CUDA GPU.不,你不能,CuDNN 需要使用 CUDA GPU。 You have to replace your CuDNNLSTM layers with standard LSTM ones.你必须用标准的 LSTM 层替换你的 CuDNNLSTM 层。

short answer: Yes, you can.简短的回答:是的,你可以。

Just need to re create your architecture with LSTM layer instead of CuDNNLSTM.只需要使用 LSTM 层而不是 CuDNNLSTM 重新创建您的架构。

Your code should be the following:您的代码应如下所示:

x = LSTM(128, return_sequences=True, recurrent_activation='sigmoid')(inputs)
x = Dropout(0.2)(x)
x = LSTM(128, return_sequences=False, recurrent_activation='sigmoid')(x)
x = Dropout(0.2)(x)
predictions = Dense(1, activation='tanh')(x)
model = Model(inputs=inputs, outputs=predictions)

and then然后

model.load_weights(path_to_your_weights_file)

Notice the recurrent_activation='sigmoid' .注意recurrent_activation='sigmoid' This is very important.这是非常重要的。

Long explanation:长解释:

LSTM and CuDNNLSTM are compatible eacth other, so you can load the weigths from one to another with no problem. LSTM 和 CuDNNLSTM 相互兼容,因此您可以毫无问题地将权重从一个加载到另一个。 However, their default values for the activation function are slightly different.但是,它们的激活函数的默认值略有不同。 Some times it leads to small differences between one and the other, but it may lead to very big ones, as reported here .有时它会导致一个和另一个之间的微小差异,但它可能会导致非常大的差异,如此处所述

[The activation values] for CuDNNLSTM [...] are hard-coded in CuDNN and cannot be changed from Keras. CuDNNLSTM [...] 的 [激活值] 是在 CuDNN 中硬编码的,不能从 Keras 更改。 They correspond to activation='tanh' and recurrent_activation='sigmoid' (slightly different than default hard_sigmoid in [LSTM] Keras).它们对应于 activation='tanh' 和 recurrent_activation='sigmoid'(与 [LSTM] Keras 中的默认 hard_sigmoid 略有不同)。 ref 参考

The easiest way to solve this would be to Replace the CuDNN layers with regular keras layer, ie.解决这个问题的最简单方法是用常规 keras 层替换 CuDNN 层,即。 convert CudNNLSTM to LSTM ,etc将 CudNNLSTM 转换为 LSTM 等

If using Google Colab go to Runtime>Change Runtime Settings and set the accelerator to GPU如果使用 Google Colab,请转到运行时>更改运行时设置并将加速器设置为GPU

You definitely can train on CuDNNLSTM, then run inference on LSTM.你绝对可以在 CuDNNLSTM 上训练,然后在 LSTM 上运行推理。

The trick is to change your layer architecture from CuDNNLSTM to LSTM in your .json file, before loading your h5 file.诀窍是在加载 h5 文件之前,将 .json 文件中的层架构从 CuDNNLSTM 更改为 LSTM。 The 2x bias in the CuDNNLSTM weights will automatically convert when you load your h5 file, but Keras won't automatically change your .json file for you.当您加载 h5 文件时,CuDNNLSTM 权重中的 2x 偏差将自动转换,但 Keras不会自动为您更改 .json 文件。

In other words, just open up your saved .json model, change all instances of CuDNNLSTM to LSTM, save the .json file, then load your .h5 file.换句话说,只需打开您保存的 .json 模型,将 CuDNNLSTM 的所有实例更改为 LSTM,保存 .json 文件,然后加载您的 .h5 文件。 You should then be able to run inference with the model.然后,您应该能够对模型进行推理。

试试

pip install tensorflow-gpu

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM