【发布时间】: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