我有一些文本,我想要执行NLP。为此,我下载了一个预先训练过的令牌器,如下所示:
import transformers as ts
pr_tokenizer = ts.AutoTokenizer.from_pretrained('distilbert-base-uncased', cache_dir='tmp')
然后,我使用如下数据创建了自己的令牌程序:
from tokenizers import Tokenizer
from tokenizers.models import BPE
tokenizer = Tokenizer(BPE(unk_token="[UNK]"))
from tokenizers.trainers import BpeTrainer
trainer = BpeTrainer(special_tokens=["[UNK]", "[CLS]", "[SEP]", "[PAD]", "[MASK]"])
from tokenizers.pre_tokenizers import Whitespace
tokenizer.pre_tokenizer = Whitespace()
tokenizer.train(['transcripts.raw'], trainer)
现在我感到困惑..。我需要更新预置令牌器(pr_tokenizer
)中的条目,其中的键与我的令牌器(tokenizer
)中的键相同。我尝试过几种方法,以下是其中之一:
new_vocab = pr_tokenizer.vocab
v = tokenizer.get_vocab()
for i in v:
if i in new_vocab:
new_vocab[i] = v[i]
那我现在该怎么办?我在想:
pr_tokenizer.vocab.update(new_vocab)
或
pr_tokenizer.vocab = new_vocab
两样都不管用。有人知道这样做的好方法吗?
发布于 2021-11-02 10:48:03
为此,您只需从GitHub或HuggingFace网站下载令牌程序源代码到与代码相同的文件夹中,然后在加载令牌程序之前编辑词汇表:
new_vocab = {}
# Getting the vocabulary entries
for i, row in enumerate(open('./distilbert-base-uncased/vocab.txt', 'r')):
new_vocab[row[:-1]] = i
# your vocabulary entries
v = tokenizer.get_vocab()
# replace common (your code)
for i in v:
if i in new_vocab:
new_vocab[i] = v[i]
with open('./distilbert-base-uncased/vocabb.txt', 'w') as f:
# reversed vocabulary
rev_vocab = {j:i for i,j in zip(new_vocab.keys(), new_vocab.values())}
# adding vocabulary entries to file
for i in range(len(rev_vocab)):
if i not in rev_vocab: continue
f.write(rev_vocab[i] + '\n')
# loading the new tokenizer
pr_tokenizer = ts.AutoTokenizer.from_pretrained('./distilbert-base-uncased')
发布于 2021-11-02 02:07:57
如果你能在你的电脑里找到蒸馏器文件夹,你可以看到词汇表基本上是txt文件,它只包含一个列。你可以做你想做的事。
# i download the model with pasting this line to python terminal (or your main cli)
# git clone https://huggingface.co/distilbert-base-uncased
import os
path= "C:/Users/...../distilbert-base-uncased"
print(os.listdir(path))
# ['.git',
# '.gitattributes',
# 'config.json',
# 'flax_model.msgpack',
# 'pytorch_model.bin',
# 'README.md', 'rust_model.ot',
# 'tf_model.h5',
# 'tokenizer.json',
# 'tokenizer_config.json',
# 'vocab.txt']
https://stackoverflow.com/questions/69780823
复制相似问题