【问题标题】:Change output shape of two merged layers in Keras更改 Keras 中两个合并层的输出形状
【发布时间】:2019-04-30 17:09:12
【问题描述】:

我正在尝试创建一个参数挖掘模型,该模型将两个句子作为输入并返回一个输出,无论这两个是神经的、不同意的还是同意的。该模型是用两个双向 LSTM 层构建的,它们合并成一个层,Model can be seen here。问题是输出层的形状是 (timestamps, label) 我希望它是形状 (1, label) 的单个输出。

    LstmLayer = LSTM(32, activation='tanh', return_sequences=False)
    left_input = Input(shape=(1000,), dtype='float32', name='left_input')
    right_input = Input(shape=(1000,), dtype='float32', name='right_input')

    left = Embedding(len(self.word_index) + 1,
                     100,
                     weights=[self.embeddingMatrix],
                     input_length=1000,
                     trainable=False)(left_input)
    left = Bidirectional(LstmLayer, merge_mode='concat')(left)

    right = Embedding(len(self.word_index) + 1,
                     100,
                     weights=[self.embeddingMatrix],
                     input_length=1000,
                     trainable=False)(right_input)
    right = Bidirectional(LstmLayer, merge_mode='concat')(right)

    merged = kl.concatenate([left, right], axis=1)
    merged = Dense(32, activation='tanh')(merged)
    main_output = Dense(self.OUTPUT_SIZE, activation='softmax', name='main_output')(merged)
    self.model = Model(inputs=[left_input, right_input], outputs=[main_output])

模型总结如下:

    Layer (type)                     Output Shape      Param #         Connected to                     
    =====================================================================
    left_input (InputLayer)         (None, 1000)         0            
    right_input (InputLayer)        (None, 1000)         0                    
    embedding_1 (Embedding)         (None, 1000, 100)    629600      left_input[0][0]
    embedding_2 (Embedding)         (None, 1000, 100)    629600      right_input[0][0]
    bidirectional_1 (Bidirectional) (None, 64)           34048       embedding_1[0][0]
    bidirectional_2 (Bidirectional) (None, 64)           34048       embedding_2[0][0]
    concatenate_1 (Concatenate)     (None, 128)          0           bidirectional_1[0][0]
                                                                     bidirectional_2[0][0]
    dense_1 (Dense)                 (None, 32)           4128        concatenate_1[0][0]
    main_output (Dense)             (None, 3)            99          dense_1[0][0]  
    ======================================================================
    Total params: 1,331,523
    Trainable params: 72,323
    Non-trainable params: 1,259,200

预测函数将两个句子编码为相同的长度,并使用模型使用的预训练分词器对它们进行分词。

 pred = self.model.predict([self._encode_sentences(sentence1), self._encode_sentences(sentence2)])
    return pred

如果我预测(“我在看一只狗”,“我在看一只猫”),则输出形状为 (21,3)

【问题讨论】:

  • 我不明白这里有什么问题?!模型的输出具有(None, 3) 的形状,因此这意味着将每个样本分为三个类别之一,这就是您想要的。
  • @Jakobo06,你明白timestamps=1000,对吧?

标签: python tensorflow keras


【解决方案1】:

你可能想要什么:

我希望它是形状为 (1, label) 的单个输出。

您可以通过获取输出序列中的最后一个输出来获取它,即result_matrix[-1]

pred = self.model.predict([self._encode_sentences(sentence1), self._encode_sentences(sentence2)])
return pred[-1]

因为这是使用给定的两个输入句子中的任何一个进行模型预测的最后状态

【讨论】:

    猜你喜欢
    • 2021-06-05
    • 1970-01-01
    • 1970-01-01
    • 2020-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-15
    相关资源
    最近更新 更多