Tensorflow CuDNNLSTM:调用nRNNFORWARD失败
我在尝试使用CuDNNLSTM而不是keras.layers.LSTM时遇到了一个问题 这就是我得到的错误: 无法使用模型配置调用rnn前进:[rnn\U模式, rnn\u输入模式,rnn\u方向模式]:2,0,0,[num\u层, 输入大小、数量单位、方向计数、顺序长度、批次大小]:[1300, 512,15521128][{{{node双向_1/cudnrnn_1}}= CudnnRNN[T=DT_FLOAT,_class=[“loc:@train…NNBackprop”], direction=“单向”,dropout=0,input\u mode=“linear\u input”, 训练是否为真,rnn_mode=“lstm”,seed=87654321,seed2=0, _device=“/job:localhost/replica:0/task:0/device:GPU:0”](双向_1/transpose_1, 双向_1/ExpandDims_1,双向_1/ExpandDims_1, 双向_1/concat_1)][{{{node loss/mul/_75}= _Recvclient_terminated=false,recv_device=“/job:localhost/replica:0/task:0/device:CPU:0”, send_device=“/job:localhost/replica:0/task:0/device:GPU:0”, 发送\u设备\u化身=1,张量\u name=“edge\u 1209\u丢失/mul”, 张量类型=DT浮点数, _device=“/job:localhost/replica:0/task:0/device:CPU:0”]] 此外,我在其中一次运行中遇到此错误: 内部错误:GPU同步失败 每次运行后,内核都会死掉 我只是在尝试使用CuDNNLSTM在google cloud上的VM实例上运行时才开始出现此错误 我的代码是:Tensorflow CuDNNLSTM:调用nRNNFORWARD失败,tensorflow,keras,google-cloud-platform,gpu,lstm,Tensorflow,Keras,Google Cloud Platform,Gpu,Lstm,我在尝试使用CuDNNLSTM而不是keras.layers.LSTM时遇到了一个问题 这就是我得到的错误: 无法使用模型配置调用rnn前进:[rnn\U模式, rnn\u输入模式,rnn\u方向模式]:2,0,0,[num\u层, 输入大小、数量单位、方向计数、顺序长度、批次大小]:[1300, 512,15521128][{{{node双向_1/cudnrnn_1}}= CudnnRNN[T=DT_FLOAT,_class=[“loc:@train…NNBackprop”], directi
MAX_LEN = max(len(article) for article in X_train_tokens)
EMBEDDING_DIM=300
vocab_size = len(word_to_id)
classes = 2
# Text input
text_input = Input(shape=(MAX_LEN,))
embedding = Embedding(vocab_size, EMBEDDING_DIM, input_length=MAX_LEN)(text_input)
x = Bidirectional(LSTM(512, return_sequences=False))(embedding)
pred = Dense(2, activation='softmax')(x)
model = Model(inputs=[text_input],outputs=pred)
model.compile(loss='categorical_crossentropy', optimizer='RMSprop', metrics=['accuracy'])
batch_size = 128
generator = text_training_generator(batch_size)
steps = len(X_train)/ batch_size
model.fit_generator(generator, steps_per_epoch=steps, verbose=True, epochs=10)
模型摘要:
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
input_1 (InputLayer) (None, 5521) 0
_________________________________________________________________
embedding_1 (Embedding) (None, 5521, 300) 8099100
_________________________________________________________________
bidirectional_1 (Bidirection (None, 1024) 3330048
_________________________________________________________________
dense_1 (Dense) (None, 2) 2050
=================================================================
Total params: 11,431,198
Trainable params: 11,431,198
Non-trainable params: 0
_________________________________________________________________
可能您的gpu内存不足。您的网络非常庞大,有1100万个可培训参数。你真的需要一个512*2的循环层输出吗 此外,你的词汇量也相当大,而你的词汇量相当小,只有5千个单词。我想你的人际网络对于你的问题来说太复杂了。我建议首先尝试嵌入大小为32和LSTM大小为32。如果您的准确性仍然很差,您可以增加复杂性
EMBEDDING_DIM = 32
Bidirectional(LSTM(32, return_sequences=False))(embedding)
最近,我的模型和Tensorflow 2.4.1也面临着这个问题;我还发现它是可复制的,例如教程中的模型。在CPU上运行(并消耗~3 GB RAM),在内存为8 GB的GPU上训练失败,错误为
2021-02-12 18:45:48.482327: E tensorflow/stream_executor/dnn.cc:616] CUDNN_STATUS_EXECUTION_FAILED
in tensorflow/stream_executor/cuda/cuda_dnn.cc(1859): 'cudnnRNNForwardTraining( cudnn.handle(), rnn_desc.handle(), model_dims.max_seq_length, input_desc.handles(), input_data.opaque(), input_h_desc.handle(), input_h_data.opaque(), input_c_desc.handle(), input_c_data.opaque(), rnn_desc.params_handle(), params.opaque(), output_desc.handles(), output_data->opaque(), output_h_desc.handle(), output_h_data->opaque(), output_c_desc.handle(), output_c_data->opaque(), workspace.opaque(), workspace.size(), reserve_space.opaque(), reserve_space.size())'
2021-02-12 18:45:48.482405: W tensorflow/core/framework/op_kernel.cc:1763] OP_REQUIRES failed at cudnn_rnn_ops.cc:1521 : Internal: Failed to call ThenRnnForward with model config: [rnn_mode, rnn_input_mode, rnn_direction_mode]: 3, 0, 0 , [num_layers, input_size, num_units, dir_count, max_seq_length, batch_size, cell_num_units]: [1, 256, 1024, 1, 100, 32, 0]
我还观察到GPU内存在出现错误之前已达到model.compile()
调用的极限
我通过添加
gpu_devices = tf.config.experimental.list_physical_devices("GPU")
for device in gpu_devices:
tf.config.experimental.set_memory_growth(device, True)
在脚本中足够早(例如,在
之后将tensorflow导入为tf
)。这指示Tensorflow按需分配GPU内存。这样,训练在GPU上运行,只消耗约2.2GB内存。我得到了相同的错误,但只适用于predict
。对于训练来说,它工作得很好。这怎么可能?你是再次加载模型还是使用已经加载到内存中的模型?我使用已经加载到内存中的模型是因为其他问题,但我确实使用load\u weights
重新加载权重。我猜是权重重新加载,但听起来有点奇怪