我想使用在[berT Embeddings] https://github.com/UKPLab/sentence-transformers中找到的预训练模型,并且我想添加一层以从模型中获取句子嵌入并传递到下一层。我该如何处理?
输入将是一个文档数组,每个文档都包含一个句子数组。
模型本身的输入是一个句子列表,它将返回嵌入列表。
这是我尝试过的但无法解决错误的方法:
def get_embeddings(input_data):
input_embed = []
for doc in input_data:
doc = tf.unstack(doc)
doc_arr = asarray(doc)
doc = [el.decode('UTF-8') for el in doc_arr]
doc = list(doc)
assert(type(doc)== list)
new_doc = []
for sent in doc:
sent = tf.unstack(sent)
new_doc.append(str(sent))
assert(type(sent)== str)
embedding= bert_model.encode(new_doc) # accepts lists of strings to return berT sentence embeddings
input_embed.append(np.array(embedding))
return tf.convert_to_tensor(input_embed,dtype=float)
sentences = tf.keras.layers.Input(shape=(3,5)) #test shape
sent_embed = tf.keras.layers.Lambda(get_embeddings)
x = sent_embed(sentences)