nlp - 需要一个“词袋”类型的变压器
问题描述
我有一个 NLP 项目,其中一组单词当前由 编码w2v
,以与其他单词集合进行比较。我想尝试transformers
哪个可以提供比w2v
. 但是,由于数据的性质,我根本不需要位置编码(因为单词的集合没有顺序)。Is there a pretrained transformer that won't do positional encoding
?
解决方案
您可以使用get_input_embeddings()访问相应的嵌入层。请看一下 roberta 的这个例子:
import torch
from transformers import RobertaTokenizerFast, RobertaModel
t = RobertaTokenizerFast.from_pretrained('roberta-base')
m = RobertaModel.from_pretrained('roberta-base')
e = m.get_input_embeddings()
myWordCollection = ['This', 'That', 'stackoverflow', 'huggingface']
#some of the words will consist of several tokens (i.e. several vectors)
i = t(myWordCollection, return_attention_mask=False, add_special_tokens=False)
#a dictionary with words:vectors for each token
o = {word:e(torch.tensor(ids)) for word, ids in zip(myWordCollection, i.input_ids)}
推荐阅读
- python - 为什么我在管理面板中创建的 Post 模型没有显示在管理面板中?
- javascript - 未捕获的类型错误:无法读取 chrome 未注册的 null 属性“classList”(试图显示和消失标题)
- python - 以 Python 变量为参数的 Shell 脚本
- python - 如何使用 xpath 创建循环?
- matlab - 错误:在 NSGA II 中,此操作的数组大小不兼容
- service - 如何使用prometheus operator service monitor监控kubernetes pod中未运行的外部服务
- amazon-web-services - 使用 AWS CLI,我如何获得显示我的 API 网关已服务多少请求的数字?
- spring - Okta 无效令牌
- python - 序列后的python字符串连接
- android - Firestore 搜索数组包含多个值 Android