对于Tensorflow 2.0上基于LSTMbased的Seq2Seq,推理期间解码器模型上的InvalidArgumentError无效

2024-04-26 03:47:54 发布

您现在位置:Python中文网/ 问答频道 /正文

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

全款

enter image description here

编码器推理模型

enter image description here

解码器推理模型

enter image description here

推断设置(实际发生错误的行)

重要信息:序列被右填充到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))并设置掩码


Tags: toon错误body单词selectpredictinitial
1条回答
网友
1楼 · 发布于 2024-04-26 03:47:54

从您的模型架构中我可以看到,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_2input 1应该来自你的initial_state张量(我们知道它有一个形状(24, 100))。这个input 2似乎来自你的empty_target_sequence,它的形状(1, 1)可以是broadcasted(1, 100)。顺便说一句,奇怪的是它没有广播给(24, 100),因为两个维度的大小都是1。。。你知道吗

我建议你在张力板上检查一下你的图表。您应该能够找到混乱的操作并跟踪其输入张量。你知道吗

相关问题 更多 >

    热门问题