我正在tensorflow 2.0上使用keras图层来构建简单的基于LSTM的Seq2Seq模型以生成文本。
版本我正在使用:Python 3.6.9,Tensorflow 2.0.0,CUDA 10.0,CUDNN 7.6.1,Nvidia驱动程序版本410.78。
我知道criteria needed by TF to delegate to CUDNNLstm
when a GPU is present(我有GPU ,并且我的模型/数据符合所有这些条件)。
培训进展顺利(带有警告消息,请参阅本文结尾),我可以验证是否正在使用CUDNNLstm。
但是,当我尝试在推断时间致电encoder_model.predict(input_sequence)
时,会收到以下错误消息:
UnknownError: [_Derived_] CUDNN_STATUS_BAD_PARAM
in tensorflow/stream_executor/cuda/cuda_dnn.cc(1424): 'cudnnSetRNNDataDescriptor( data_desc.get(), data_type, layout, max_seq_length, batch_size, data_size, seq_lengths_array, (void*)&padding_fill)'
[[{{node cond/then/_0/CudnnRNNV3}}]]
[[lstm/StatefulPartitionedCall]] [Op:__inference_keras_scratch_graph_91878]
Function call stack:
keras_scratch_graph -> keras_scratch_graph -> keras_scratch_graph
这里是训练代码:(source_sequences
和target_sequences
都是右填充序列,而嵌入矩阵是预训练的Glove嵌入)
# Define an input sequence and process it.
encoder_inputs = tf.keras.layers.Input(shape=(24,))
encoder_embedding_layer = tf.keras.layers.Embedding(
VOCABULARY_SIZE_1,
EMBEDDING_DIMS,
embeddings_initializer=initializers.Constant(encoder_embedding_matrix),
mask_zero=True)
encoder_embedding = encoder_embedding_layer(encoder_inputs)
_, state_h, state_c = tf.keras.layers.LSTM(
EMBEDDING_DIMS,
implementation=1,
return_state=True)(encoder_embedding)
encoder_states = [state_h, state_c]
decoder_inputs = tf.keras.layers.Input(shape=(24,))
decoder_embedding_layer = tf.keras.layers.Embedding(
VOCABULARY_SIZE_2,
EMBEDDING_DIMS,
embeddings_initializer=initializers.Constant(decoder_embedding_matrix),
mask_zero=True)
decoder_embedding = decoder_embedding_layer(decoder_inputs)
decoder_lstm = tf.keras.layers.LSTM(
EMBEDDING_DIMS,
return_sequences=True,
return_state=True,
implementation=1)
decoder_outputs, _, _ = decoder_lstm(decoder_embedding, initial_state=encoder_states)
decoder_dense = tf.keras.layers.Dense(VOCABULARY_SIZE_TITLE, activation='softmax')
output = decoder_dense(decoder_outputs)
model = tf.keras.models.Model([encoder_inputs, decoder_inputs], output)
model.compile(optimizer='rmsprop', loss='sparse_categorical_crossentropy')
model.summary()
model.fit([source_sequences, target_sequences], decoder_target_data,
batch_size=32,
epochs=10,
validation_split=0.0,
verbose=2)
这些是推理模型:
encoder_model = tf.keras.models.Model(encoder_inputs, encoder_states)
decoder_state_input_h = tf.keras.layers.Input(shape=(input_dimension ,))
decoder_state_input_c = tf.keras.layers.Input(shape=(input_dimension ,))
decoder_states_inputs = [decoder_state_input_h, decoder_state_input_c]
decoder_outputs, state_h, state_c = decoder_lstm_layer(
decoder_embedding_layer , initial_state=decoder_states_inputs)
decoder_states = [state_h, state_c]
decoder_outputs = output_layer(decoder_outputs)
decoder_model = tf.keras.models.Model(
[decoder_inputs] + decoder_states_inputs,
[decoder_outputs] + decoder_states)
当我在predict()
上致电encoder_model
时,我得到CUDNN_STATUS_BAD_PARAM
推断代码(触发错误的地方)
# build the initial state with a right-padded input sequence
#### CUDNN_STATUS_BAD_PARAM is TRIGGERED ON THIS LINE!!! ######## <<<<<<<<<
state = encoder_model.predict(masked_input_sequence)
empty_target_sequence = np.zeros((1,1))
# this signals the Start of sequence
empty_target_sequence[0,0] = titles_word_index[sos_token]
decoder_outputs, h, c = decoder_model.predict([empty_target_sequence] + state)
我尝试过的事情
显式创建掩码(encoder_embedding_layer.compute_mask()
),并在每次调用LSTM层时将其作为参数添加,例如:
encoder_embedding = encoder_embedding_layer(encoder_inputs)
encoder_mask = encoder_embedding_layer.compute_mask(encoder_inputs)
_, state_h, state_c = tf.keras.layers.LSTM(
EMBEDDING_DIMS,
return_state=True)(encoder_embedding,mask=encoder_mask)
不对嵌入层使用初始化程序,以查看问题是否存在
PS: 强制在CPU上进行训练会使错误消失,但我需要在GPU上进行训练,否则需要一段时间才能完成。< / p>
附言::这似乎是我遇到的错误:Masking LSTM: OP_REQUIRES failed at cudnn_rnn_ops.cc:1498 : Unknown: CUDNN_STATUS_BAD_PARAM
PS::当我在supports_masking
,model
和encoder_model
上调用方法decoder_model
时,它们全部返回False
出于某些原因。
PS:如我所说,培训已完成,没有(明显)错误,但是如果我在命令行上查看Jupyter输出日志,则可以在执行过程中看到以下警告消息培训:
2019-11-16 19:48:20.144265: W
tensorflow/core/grappler/optimizers/implementation_selector.cc:310] Skipping optimization due to error while loading function libraries:
Invalid argument: Functions '__inference___backward_cudnn_lstm_with_fallback_47598_49057' and
'__inference___backward_cudnn_lstm_with_fallback_47598_49057_specialized_for_StatefulPartitionedCall_1_at___inference_distributed_function_52868'
both implement 'lstm_d41d5ccb-14be-4a74-b5e8-cc4f63c5bb02' but their signatures do not match.