gpt4 book ai didi

nlp - 如何使用转换器模型获取词汇外单词的词嵌入?

转载 作者:行者123 更新时间:2023-12-04 08:16:06 25 4
gpt4 key购买 nike

当我尝试使用 bio_clinical bert 获取句子的词嵌入时,对于 8 个词的句子,我得到了 11 个标记 ID(+开始和结束),因为“嵌入”是词汇外的词/标记,它被拆分为em , bed , ding , s .
我想知道除了计算这些向量的平均值之外,是否有任何可用的聚合策略有意义。

from transformers import AutoTokenizer, AutoModel
# download and load model
tokenizer = AutoTokenizer.from_pretrained("emilyalsentzer/Bio_ClinicalBERT")
model = AutoModel.from_pretrained("emilyalsentzer/Bio_ClinicalBERT")

sentences = ['This framework generates embeddings for each input sentence']


#Tokenize sentences
encoded_input = tokenizer(sentences, padding=True, truncation=True, max_length=128, return_tensors='pt')


#Compute token embeddings
with torch.no_grad():
model_output = model(**encoded_input)

print(encoded_input['input_ids'].shape)
输出: torch.Size([1, 13])
for token in encoded_input['input_ids'][0]:
print(tokenizer.decode([token]))
输出:
[CLS]
this
framework
generates
em
##bed
##ding
##s
for
each
input
sentence
[SEP]

最佳答案

据我所知,平均聚合是这里最常用的工具,实际上甚至有科学文献,经验表明它运作良好:
Generalizing Word Embeddings using Bag of Subwords赵、穆加尔和梁。公式 1 也准确描述了您的提议。
理论上您可以采用的一种替代方法是对整个输入进行平均聚合,本质上是对所有单词进行“上下文预测”(可能除了“embeddings”),因此模拟类似于 [MASK] 的东西。在变压器模型的训练期间。但这只是我的建议,没有任何科学证据证明它有效(无论好坏)。

关于nlp - 如何使用转换器模型获取词汇外单词的词嵌入?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65696968/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com