versions: Python 3.6.9, Tensorflow 2.0.0, CUDA 10.0, CUDNN 7.6.1, Nvidia driver version 410.78.
我正在尝试移植一个基于LSTM的Seq2Seq克拉斯特遣部队tensorflow 2.0模型
现在,当我试图调用解码器模型上的predict
时,我面临以下错误(请参阅下面的实际推断设置代码)
就好像它期望一个单词作为参数,但我需要它来解码一个完整的句子(我的句子是单词索引的右填充序列,长度为24)
P.S.: This code used to work exactly as it is on TF 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
全款
编码器推理模型
解码器推理模型
重要信息:序列被右填充到24元素,100是每个单词嵌入的维数。这就是为什么错误消息(和打印)显示输入形状是(24,100)
。你知道吗
note that this code runs on a CPU. running it on a GPU leads to another error detailed 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] != input 1: [24,100]
decoder_outputs, h, c = decoder_model.predict([empty_target_sequence] + initial_state)
我尝试过的事情
禁用渴望模式(这只会使训练速度大大减慢,推理过程中的错误保持不变)
在将输入输入输入到predict函数之前对其进行整形
调用LSTM层时手动计算(embedding_layer.compute_mask(inputs)
)并设置掩码
从您的模型架构中我可以看到,
initial_state
是一个具有形状的张量数组:[(?, 100), (?, 100), (?, 100)]
。在您的例子中,未知维度固定为24。你知道吗然后,构建一个
(1, 1)
形状的Numpy数组/TF张量。您可以将它包装在一个列表中并附加initial_state
。因此,您将得到一个带有形状的张量列表:[(1, 1), (?, 100), (?, 100), (?, 100)]
。你知道吗您尝试将其作为输入传递给解码器模型,该模型需要3个输入(一个输入列表)和
[(?, 24), (?, 100), (?, 100)]
形状。你知道吗从那开始似乎有什么不对劲。。。你知道吗
但是,TF抱怨操作的输入
while/body/_1/Select_2
。input 1
应该来自你的initial_state
张量(我们知道它有一个形状(24, 100)
)。这个input 2
似乎来自你的empty_target_sequence
,它的形状(1, 1)
可以是broadcasted到(1, 100)
。顺便说一句,奇怪的是它没有广播给(24, 100)
,因为两个维度的大小都是1。。。你知道吗我建议你在张力板上检查一下你的图表。您应该能够找到混乱的操作并跟踪其输入张量。你知道吗
相关问题 更多 >
编程相关推荐