【问题标题】:How to i get word embeddings for out of vocabulary words using a transformer model?如何使用转换器模型获取词汇表外单词的词嵌入?
【发布时间】:2021-04-18 04:09:05
【问题描述】:

当我尝试使用 bio_clinical bert 获取句子的词嵌入时,对于 8 个单词的句子,我得到 11 个标记 id(+start 和 end),因为“embeddings”是词汇表外的单词/标记,即被分成embeddings

我想知道除了对这些向量进行平均之外,是否有任何可用的聚合策略有意义。

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]

【问题讨论】:

    标签: nlp huggingface-transformers transformer huggingface-tokenizers


    【解决方案1】:

    据我所知,均值聚合是这里最常用的工具,实际上甚至有科学文献,经验表明它运作良好: Generalizing Word Embeddings using Bag of Subwords 赵、Mudgal 和梁。公式 1 也准确地描述了您的提议。

    理论上您可以采用的一种替代方法是整个输入的平均聚合,本质上是对所有单词(可能除了“embeddings”)进行“上下文预测”,因此在训练变压器模型期间模拟类似于[MASK]ing 的东西。但这只是我的一个建议,没有任何科学证据证明它有效(无论好坏)。

    【讨论】:

      猜你喜欢
      • 2018-09-04
      • 2018-08-21
      • 2016-10-07
      • 2019-01-11
      • 1970-01-01
      • 1970-01-01
      • 2021-11-30
      • 2023-03-31
      • 1970-01-01
      相关资源
      最近更新 更多