BertTokenizer-编码和解码序列时会出现多余的空格

使用HuggingFace的《变形金刚》时,我遇到了编码和解码方法的问题。

我有以下字符串:

test_string = 'text with percentage%'

然后我正在运行以下代码:

import torch
from transformers import bertTokenizer

tokenizer = bertTokenizer.from_pretrained('bert-base-cased')

test_string = 'text with percentage%'

# encode Converts a string in a sequence of ids (integer),using the tokenizer and vocabulary.
input_ids = tokenizer.encode(test_string)
output = tokenizer.decode(input_ids)

输出看起来像这样:

'text with percentage %'

%之前有多余的空格。我尝试了像clean_up_tokenization_spaces这样的额外参数,但这是不同的东西。

如何在解码和编码中使用什么来获得前后完全相同的文本。其他特殊标志也会发生这种情况。

yanglinice 回答:BertTokenizer-编码和解码序列时会出现多余的空格

根据https://github.com/huggingface/transformers/pull/1274,他们正在努力。希望下周某个时候有解决方案。

,

如果您尝试使用 BERT 进行令牌分类以在原始字符串中找到一个跨度,那么一种解决方法是使用带有选项 BertTokenizerFastreturn_offsets_mapping=True

test_string = 'text with percentage%'

tokenizer = BertTokenizerFast.from_pretrained('bert-base-cased')
tokens = tokenizer(test_string,return_offsets_mapping=True)
input_ids = tokens.data["input_ids"]

span_start_index,span_stop_index = some_model(input_ids)

然后一旦你得到token分类结果,你就可以做类似的事情

predicted_span = test_string[tokens.encodings[0].offsets[span_start_index][0]:tokens.encodings[0].offsets[span_stop_index][1]]
本文链接:https://www.f2er.com/3056328.html

大家都在问