将BERT句子嵌入到暹罗LSTM网络中

时间:2019-07-11 02:28:44

标签: keras deep-learning nlp lstm sentence-similarity

我正在研究一个文本相似性项目,我想尝试一个暹罗LSTM网络。我正在修改此实现https://amitojdeep.github.io/amitoj-blogs/2017/12/31/semantic-similarity.html。该代码基于使用Word2Vec单词嵌入,我想将其替换为BERT句子嵌入https://github.com/imgarylai/bert-embedding

结果矩阵的第1列具有输入的句子字符串,第2列的每个单元格都包含相应的嵌入矩阵(num_words,768)。我的理解是,使用此嵌入矩阵,我可以简单地跳过嵌入层步骤,并将其直接替换为下一层。这是我在下面尝试做的。

# The visible layer

left_input = Input(shape=(max_seq_length,), dtype='int32')
right_input = Input(shape=(max_seq_length,), dtype='int32')

#embedding_layer = Embedding(len(embeddings), embedding_dim, weights=[embeddings], input_length=max_seq_length, trainable=False)
#embedding layer modified to use embeddings from BERT

# Embedded version of the inputs  
encoded_left_full = bert_embedding(left_input) 
encoded_left = encoded_left_full[0][1]
encoded_right_full = bert_embedding(right_input)
encoded_right = encoded_right_full[0][1]

我收到以下错误消息:

  

TypeError:类型为“ Tensor”的对象没有len()。

我的理解是,由于我采用了输入张量,但是bert_embedding函数直接使用字符串,因此存在类型不匹配的情况。请为我提供可能的解决方案的建议,例如直接将字符串作为输入或任何其他修改。

0 个答案:

没有答案