如何在Keras中使用我自己的句子?

如何在Keras中使用我自己的句子?,keras,nlp,lstm,word-embedding,sentence-similarity,Keras,Nlp,Lstm,Word Embedding,Sentence Similarity,我是Keras的新手,我创建了自己的tf_idf句子嵌入形状(无句子,嵌入dim)。我试图将此矩阵作为输入添加到LSTM层。我的网络看起来像这样: q1_tfidf = Input(name='q1_tfidf', shape=(max_sent, 300)) q2_tfidf = Input(name='q2_tfidf', shape=(max_sent, 300)) q1_tfidf = LSTM(100)(q1_tfidf) q2_tfidf = LSTM(100)(q2_tfidf)

我是Keras的新手,我创建了自己的tf_idf句子嵌入形状(无句子,嵌入dim)。我试图将此矩阵作为输入添加到LSTM层。我的网络看起来像这样:

q1_tfidf = Input(name='q1_tfidf', shape=(max_sent, 300))
q2_tfidf = Input(name='q2_tfidf', shape=(max_sent, 300))

q1_tfidf = LSTM(100)(q1_tfidf)
q2_tfidf = LSTM(100)(q2_tfidf)
distance2 = Lambda(preprocessing.exponent_neg_manhattan_distance, output_shape=preprocessing.get_shape)(
        [q1_tfidf, q2_tfidf])
我正在努力研究矩阵的形状。我得到这个错误:

ValueError: Error when checking input: expected q1_tfidf to have 3 dimensions, but got array with shape (384348, 300)
我已经查过这篇文章了,但还是不明白。我好像错过了一些明显的东西


你知道怎么做吗?

好的,据我所知,你想预测两句话之间的区别。 如何重用LSTM层(语言模型应相同),只需学习一个句子嵌入并使用两次:

q1_tfidf = Input(name='q1_tfidf', shape=(max_sent, 300))
q2_tfidf = Input(name='q2_tfidf', shape=(max_sent, 300))

lstm = LSTM(100)

lstm_out_q1= lstm (q1_tfidf)
lstm_out_q2= lstm (q2_tfidf)
predict = concatenate([lstm_out_q1, lstm_out_q2])
model = Model(inputs=[q1_tfidf ,q1_tfidf ], outputs=predict)

predict = concatenate([q1_tfidf , q2_tfidf])

您还可以在附加的lambda层中引入自定义距离,但因此需要在连接中使用不同的整形。

您希望实现什么?您的数据是什么样子的?我的数据是这样的:(问题1,问题2,标签)其中标签显示问题是否是意译。我的想法是为q1和q2创建句子嵌入,计算两种编码之间的相似性度量,并将其作为神经网络中的一个功能来预测这对编码是否重复。谢谢!我也意识到我的输入是错误的。应该是这样的:
q1\u tfidf=Input(name='q1\u tfidf',shape=(1300,))q2\u tfidf=Input(name='q2\u tfidf',shape=(1300,)