在使用 HuggingFace 的 Transformers 时,我遇到了编码和解码方法的问题。
我有以下字符串:
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
但这是为了不同的东西。
我应该如何在解码和编码中使用什么来获得前后完全相同的文本。这也发生在其他特殊标志上。