1. tokenizer
from transformers import AutoTokenizer
tokenizer = AutoTokenizer.from_pretrained('microsoft/deberta-base')
t1 = ' Today we released the new model: multilingual generative pre-trained transformer.'
t2 = 'The checkpoints are available on Huggingface model page.'
encoded_texts = tokenizer(
t1,
t2,
truncation='only_second',
max_length=20,
padding="max_length",
return_offsets_mapping=True,
return_token_type_ids=True
)
print(encoded_texts)
====================================
{
'input_ids': [1, 2477, 52, 703, 5, 92, 1421, 35, 7268, 41586, 20181, 3693, 1198, 12, 23830, 40878, 4, 2, 133, 2],
'token_type_ids': [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
'attention_mask': [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
'offset_mapping': [(0, 0), (0, 6), (6, 9), (9, 18), (18, 22), (22, 26), (26, 32), (32, 33), (33, 38), (38, 46), (46, 52), (52, 57), (57, 61), (61, 62), (62, 69), (69, 81), (81, 82), (0, 0), (0, 3), (0, 0)]
}
-
input_ids
: 对应词汇表id -
token_type_ids
: token 对应的句子id,值为0或1(0表示对应的token属于第一句,1表示属于第二句)。形状为(batch_size, sequence_length)。 -
'attention_mask'
: 可选参数。各元素的值为 0 或 1 ,设置来避免在 padding 的 token 上计算 attention (1不进行masked,0则masked)。形状为(batch_size, sequence_length)。 -
'offset_mapping'
: 对应token位置和原始位置的map,即当前token在原文本中的start,end位置。注意, [start, end)左闭右开 。如例子中,'Today'->start:0 end:6。
具体来看,我们将encoded_texts['input_ids']
转为token,以及encoded_texts['offset_mapping']
如下,(忽略Ġ
)。
encoded_texts['offset_mapping']
中Today
对应中原始位置索引为从0到6。其它依次为下一个token的原始位置索引。
converted2txt = tokenizer.convert_ids_to_tokens(encoded_texts['input_ids'])
print(converted2txt)
=================================
['[CLS]', 'ĠToday', 'Ġwe', 'Ġreleased', 'Ġthe', 'Ġnew', 'Ġmodel',
':', 'Ġmult', 'ilingual', 'Ġgener', 'ative', 'Ġpre', '-', 'trained',
'Ġtransformer', '.', '[SEP]', 'The', '[SEP]']
print(encoded_texts['offset_mapping'])
=====================================
[(0, 0), (0, 6), (6, 9), (9, 18), (18, 22), (22, 26), (26, 32),
(32, 33), (33, 38), (38, 46), (46, 52), (52, 57), (57, 61),
(61, 62), (62, 69), (69, 81), (81, 82), (0, 0), (0, 3), (0, 0)]
各种bert输入格式:
bert: [CLS] + tokens + [SEP] + padding
roberta: [CLS] + prefix_space + tokens + [SEP] + padding
distilbert: [CLS] + tokens + [SEP] + padding
xlm: [CLS] + tokens + [SEP] + padding
xlnet: padding + tokens + [SEP] + [CLS]
另外,用sequence_ids
参数来区分第一句和第二句。例如,
sequence_ids = encoded_texts.sequence_ids()
print(sequence_ids)
#输出:
[None, 0, 0, 0, 0, 0, 0, 0, 0, None, 1, 1, 1, 1, 1, 1, None]
这里None
对应了special tokens
,然后0
、1
分表代表第一句和第二句。