版本:Python 3.6.9,Tensorflow 2.0.0,CUDA 10.0,CUDN 7.6.1,Nvidia驱动程序版本410.78。
我正在尝试将基于LSTM的Seq2Seq tf.keras模型移植到tensorflow 2.0
现在,当我尝试在解码器模型上调用predict
时遇到以下错误(有关实际的推理设置代码,请参见下文)
就好像在期待一个单个单词作为自变量一样,但是我需要它来解码一个完整的句子(我的句子是单词索引的右填充序列,长度为24)>
P.S。:该代码过去在
1.15版本上完全按原样工作
InvalidArgumentError: [_Derived_] Inputs to operation while/body/_1/Select_2 of type Select must have the same size and shape.
Input 0: [1,100] != input 1: [24,100]
[[{{node while/body/_1/Select_2}}]]
[[lstm_1_3/StatefulPartitionedCall]] [Op:__inference_keras_scratch_graph_45160]
Function call stack:
keras_scratch_graph -> keras_scratch_graph -> keras_scratch_graph
完整模型
ENCODER推断模型
DECODER推断模型
推理设置(实际发生错误的行)
重要信息:序列被右填充到 24 个元素,而 100 是每个单词嵌入的维数。这就是为什么错误消息(和打印件)显示输入形状为(24,100)
的原因。
请注意,此代码在 CPU 上运行。在GPU上运行它会导致另一个错误,here
# original_keyword is a sample text string
with tf.device("/device:CPU:0"):
# this method turns the raw string into a right-padded sequence
query_sequence = keyword_to_padded_sequence_single(original_keyword)
# no problems here
initial_state = encoder_model.predict(query_sequence)
print(initial_state[0].shape) # prints (24,100)
print(initial_state[1].shape) # (24,100)
empty_target_sequence = np.zeros((1,1))
empty_target_sequence[0,0] = word_dict_titles["sos"]
# ERROR HAPPENS HERE:
# InvalidArgumentError: [_Derived_] Inputs to operation while/body/_1/Select_2 of type Select
# must have the same size and shape. Input 0: [1,100]
decoder_outputs,h,c = decoder_model.predict([empty_target_sequence] + initial_state)
我尝试过的事情
-
禁用急切模式(这只会使训练慢得多,并且推理期间的错误保持不变)
-
在将输入提供给预测函数之前重塑输入
-
在调用LSTM层时手动计算(
embedding_layer.compute_mask(inputs)
)和设置掩码