更改Keras中两个合并图层的输出形状

时间:2018-11-28 17:57:50

标签: python tensorflow keras

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

    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)

1 个答案:

答案 0 :(得分:0)

您可能想要的东西:

  

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

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

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

因为这是具有给定两个输入语句中任何一个的模型预测的最后状态