使用 HuggingFace 的管道工具,我惊讶地发现使用快速和慢速分词器时输出存在显着差异。
具体来说,当我运行填充掩码管道时,分配给填充掩码的单词的概率对于快速和慢速分词器是不同的。此外,尽管无论输入的句子的数量和长度如何,快速分词器的预测都保持不变,但慢分词器的情况并非如此。
这是一个最小的例子:
from transformers import pipeline
slow = pipeline('fill-mask', model='bert-base-cased', \
tokenizer=('bert-base-cased', {"use_fast": False}))
fast = pipeline('fill-mask', model='bert-base-cased', \
tokenizer=('bert-base-cased', {"use_fast": True}))
s1 = "This is a short and sweet [MASK]." # "example"
s2 = "This is [MASK]." # "shorter"
slow([s1, s2])
fast([s1, s2])
slow([s2])
fast([s2])
Run Code Online (Sandbox Code Playgroud)
每个管道调用都会产生可以填充 for 的前 5 个标记[MASK]
,以及它们的概率。为简洁起见,我省略了实际输出,但分配给填充[MASK]
for 的每个单词的概率s2
在所有示例中都不相同。最后 3 个示例给出相同的概率,但第一个示例产生不同的概率。差异如此之大,以至于两组的前 5 名并不一致。
据我所知,这背后的原因是快速和慢速分词器返回不同的输出。快速分词器通过填充 0 将序列长度标准化为 512,然后创建一个注意力掩码来阻止填充。相比之下,慢速分词器仅填充到最长序列的长度,并且不会创建这样的注意力掩码。相反,它将填充的标记类型 id 设置为 1(而不是 0,这是非填充标记的类型)。根据我对 HuggingFace 的实现(在这里找到)的理解,这些是不等价的。
有谁知道这是否是故意的?
python nlp bert-language-model huggingface-transformers huggingface-tokenizers
我需要翻译数据库中的大量文本。因此,我这几天一直在和变压器和模型打交道。我绝对不是数据科学专家,不幸的是我没有进一步了解。
\n问题始于较长的文本。第二个问题是定序器的通常最大令牌大小 (512)。仅仅截断并不是一个真正的选择。在这里,我确实找到了一种解决方法,但它无法正常工作,结果是较长文本(> 300 个序列)上的单词沙拉
\n这是一个示例(请忽略警告,这是另一个问题 - 目前没有那么严重);
\n如果我使用例句 2(55 个序列)或 5 次(163 个序列)-没有问题。
\n但它会被例如 433 序列(屏幕截图中的第三个绿色文本块)搞乱。
\n\n对于超过 510 个序列,我尝试将其分成块,如上面描述的链接所示。但这里的结果也很奇怪。
\n我很确定 - 我有不止一个错误并且低估了这个主题。\n但我认为没有其他(免费/便宜)的方法来翻译大量文本。
\n你们能帮我一下吗?您发现哪些(思维)错误以及您建议如何解决这些问题?非常感谢。
\n\nfrom transformers import AutoTokenizer, AutoModelForSeq2SeqLM\nimport torch\n\nif torch.cuda.is_available(): \n dev = "cuda"\nelse: \n dev = "cpu" \ndevice = torch.device(dev)\n \nmname = \'Helsinki-NLP/opus-mt-de-en\'\ntokenizer = AutoTokenizer.from_pretrained(mname)\nmodel = AutoModelForSeq2SeqLM.from_pretrained(mname)\nmodel.to(device)\n\nchunksize = 512\n\ntext_short = "Nach nur sieben Seiten appellierte man an die W\xc3\xa4hlerinnen und W\xc3\xa4hler, sich richtig …
Run Code Online (Sandbox Code Playgroud) python translation huggingface-transformers huggingface-tokenizers
我正在尝试在 Google Collaboratory 上实施 XLNET。但我得到以下问题。
ImportError:
XLNetTokenizer requires the SentencePiece library but it was not found in your environment. Checkout the instructions on the
installation page of its repo: https://github.com/google/sentencepiece#installation and follow the ones
that match your environment.
Run Code Online (Sandbox Code Playgroud)
我还尝试了以下步骤:
!pip install -U transformers
!pip install sentencepiece
from transformers import XLNetTokenizer
tokenizer = XLNetTokenizer.from_pretrained('xlnet-base-cased-spiece.model')
Run Code Online (Sandbox Code Playgroud)
提前谢谢你的帮助。
transformer-model google-colaboratory huggingface-transformers huggingface-tokenizers
我正在处理一个巨大的文本数据集以进行内容分类。我已经实现了 distilbert 模型和 distilberttokenizer.from_pretrained() tokenizer。这个 tokenizer 花费了非常长的时间来对我的文本数据进行 tokenizer 大约 7 分钟,只有 14k 记录,这是因为它在我的 CPU 上运行。
有什么方法可以强制标记器在我的 GPU 上运行。
nlp deep-learning huggingface-transformers huggingface-tokenizers
BertTokenizer
可以将一个句子标记为标记列表,其中一些长单词(例如“embeddings”)被分成几个子词,即“em”、“##bed”、“##ding”和“##s”。
有没有办法找到子词?例如,
t = BertTokenizer.from_pretrained('bert-base-uncased')
tokens = t('word embeddings', add_special_tokens=False)
location = locate_subwords(tokens)
Run Code Online (Sandbox Code Playgroud)
我想要对应于 的location
be like ,其中 0 表示普通单词,1 表示子单词。[0, 1, 1, 1, 1]
['word', 'em', '##bed', '##ding', '##s']
我使用tokenizer = RobertaTokenizerFast.from_pretrained('roberta-base',add_prefix_space=True)
在英语数据上训练的 roberta-base 分词器来对孟加拉语进行分词,只是为了看看它的行为如何。当我尝试对孟加拉语字符进行编码时tokenizer.encode('\xe0\xa6\xac\xe0\xa6\xbe')
,我得到[0, 1437, 35861, 11582, 35861, 4726, 2]
的结果是,即使在英语上进行训练,它也会在词汇表中找到一些与孟加拉语字符匹配的标记。经过进一步探索,我发现这些都是特殊字符['<s>', '\xc4\xa0', '\xc3\xa0\xc2\xa6', '\xc2\xac', '\xc3\xa0\xc2\xa6', '\xc2\xbe', '</s>']
。我的问题是为什么会发生这种情况,当应用于新语言时,它不应该输出未知的标记吗?非常感谢任何帮助
huggingface-transformers huggingface-tokenizers roberta-language-model
我正在尝试安装一些 Python 包,即tokenizers
来自 Huggingface 的包transformers
,它显然需要 Rust。所以我在我的 Docker 版本上安装 Rust:
FROM nikolaik/python-nodejs\nUSER pn\nWORKDIR /home/pn/app\n\nCOPY . /home/pn/app/\nRUN ls -la /home/pn/app/*\nRUN curl https://sh.rustup.rs -sSf | sh -s -- -y\n\nENV PATH ~/.cargo/bin:$PATH\n\n# Install Python dependencies.\nRUN pip install --upgrade pip\nRUN pip install -r requirements.txt\nRUN python load_model.py\n\n# to be equal to the cores available.\nCMD exec gunicorn --bind :$PORT --workers 4 --threads 4 app:app\n
Run Code Online (Sandbox Code Playgroud)\n但是,安装分词器时似乎pip
找不到:Rust
Building wheels for collected packages: tokenizers\n#11 103.2 Building wheel for tokenizers (pyproject.toml): …
Run Code Online (Sandbox Code Playgroud) 我在 Macbook Pro M1 Max 中安装了变压器
之后,我安装了标记器
pip install tokenizers
Run Code Online (Sandbox Code Playgroud)
这显示了
使用缓存的 tokenizers-0.12.1-cp39-cp39-macosx_12_0_arm64.whl收集
tokenizers 已成功安装 tokenizers-0.12.1
它似乎对 whl 文件使用了正确的体系结构
当我导入它时我得到
'/Users/myname/miniforge3/envs/tf/lib/python3.9/site-packages/tokenizers/tokenizers.cpython-39-darwin.so'(mach-o 文件,但是不兼容的架构(具有 'x86_64' ,需要'arm64e'))
我发现这个问题以前也发生在其他人身上。关于如何解决这个问题有什么想法吗?
我尝试使用 wav2vec2 (XLSR 模型)但没有成功:
import transformers
from transformers import Wav2Vec2ForCTC, Wav2Vec2Processor
import librosa
import torch
wav2vec2_processor = Wav2Vec2Processor.from_pretrained("facebook/wav2vec2-large-xlsr-53")
wav2vec2_model = Wav2Vec2ForCTC.from_pretrained("facebook/wav2vec2-large-xlsr-53")
file_name = "test.wav"
speech, sr = librosa.load(file_name, sr=16000)
input_values = wav2vec2_processor(speech, sampling_rate=16000, return_tensors="pt").input_values
logits = wav2vec2_model(input_values).logits
Run Code Online (Sandbox Code Playgroud)
错误:
OSError: Can't load tokenizer for 'facebook/wav2vec2-large-xlsr-53'. If you were trying to load it from 'https://huggingface.co/models', make sure you don't have a local directory with the same name. Otherwise, make sure 'facebook/wav2vec2-large-xlsr-53' is the correct path to a directory containing all relevant files …
Run Code Online (Sandbox Code Playgroud) deep-learning huggingface-transformers huggingface-tokenizers huggingface
我在batch_encode_plus
标记器的方法中遇到了一个奇怪的问题。我最近从 Transformer 版本 3.3.0 切换到 4.5.1。(我正在为 NER 创建数据束)。
我有两个句子需要编码,并且我有一个句子已经被标记化的情况,但是由于这两个句子的长度不同,所以我需要pad [PAD]
较短的句子才能获得一批统一的长度。
下面是我使用 3.3.0 版本的 Transformer 所做的代码
from transformers import AutoTokenizer
pretrained_model_name = 'distilbert-base-cased'
tokenizer = AutoTokenizer.from_pretrained(pretrained_model_name, add_prefix_space=True)
sentences = ["He is an uninvited guest.", "The host of the party didn't sent him the invite."]
# here we have the complete sentences
encodings = tokenizer.batch_encode_plus(sentences, max_length=20, padding=True)
batch_token_ids, attention_masks = encodings["input_ids"], encodings["attention_mask"]
print(batch_token_ids[0])
print(tokenizer.convert_ids_to_tokens(batch_token_ids[0]))
# And the output
# [101, 1124, 1110, 1126, 8362, 1394, 5086, 1906, 3648, …
Run Code Online (Sandbox Code Playgroud) python pytorch huggingface-transformers huggingface-tokenizers huggingface-datasets
python ×3
nlp ×2
pytorch ×2
apple-m1 ×1
docker ×1
docker-build ×1
huggingface ×1
pip ×1
python-3.x ×1
rust ×1
translation ×1