[英]Get Bert Embeddings for every Token in a Sentence
我在 python 中有一個 dataframe ,其中有一列文本數據。 我需要運行一個循環,在該循環中,我將獲取該文本列中的每一行,並為該特定行中的每個標記獲取 bert 嵌入。 然后我需要 append 那些向量嵌入並出於某種目的進行嘗試。
例如“我的名字是奧巴馬”為“我”獲取 768 個向量嵌入為“姓名”獲取 768 個向量嵌入為“是”獲取 768 個向量嵌入為“奧巴馬”獲取 768 個向量嵌入
最終 output:大小為 768*4 = 3072 的向量嵌入假設每一行都有確切的單詞數
我相信您正在嘗試將句子中單個單詞的基於上下文的嵌入帶入圖片,而不是像 GloVe 那樣的固定向量。 你的方法應該是。
pip install sentence-transformers
一旦安裝;
model = SentenceTransformer('paraphrase-distilroberta-base-v1')
#Our sentences we like to encode
sentences = ['This framework generates embeddings for each input sentence',
'Sentences are passed as a list of string.',
'The quick brown fox jumps over the lazy dog.']
#Sentences are encoded by calling model.encode()
embeddings = model.encode(sentences)
#Print the embeddings
for sentence, embedding in zip(sentences, embeddings):
print("Sentence:", sentence)
print("Embedding:", embedding)
print("")
查看嵌入向量和圍繞嵌入的聚合技術。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.