tokenizer¶
-
class
MobileBertTokenizer
(vocab_file, do_lower_case=True, do_basic_tokenize=True, never_split=None, unk_token='[UNK]', sep_token='[SEP]', pad_token='[PAD]', cls_token='[CLS]', mask_token='[MASK]', tokenize_chinese_chars=True, strip_accents=None, **kwargs)[source]¶ Bases:
paddlenlp.transformers.bert.tokenizer.BertTokenizer
Construct a MobileBERT tokenizer.
BertTokenizer
and runs end-to-end tokenization: punctuation splitting and wordpiece. Refer to superclassBertTokenizer
for usage examples and documentation concerning parameters.-
batch_encode
(batch_text_or_text_pairs, max_length: int = 512, padding: Union[bool, str, paddlenlp.transformers.tokenizer_utils_base.PaddingStrategy] = False, truncation: Union[bool, str, paddlenlp.transformers.tokenizer_utils_base.TruncationStrategy] = False, stride=0, is_split_into_words=False, return_position_ids=False, return_token_type_ids=True, return_attention_mask=False, return_length=False, return_overflowing_tokens=False, return_special_tokens_mask=False, return_dict=True, pad_to_multiple_of: Optional[int] = None, return_tensors: Optional[Union[str, paddlenlp.transformers.tokenizer_utils_base.TensorType]] = None, verbose: bool = True, **kwargs)[source]¶ Performs tokenization and uses the tokenized tokens to prepare model inputs. It supports batch inputs of sequence or sequence pair.
- Parameters
batch_text_or_text_pairs (list) – The element of list can be sequence or sequence pair, and the sequence is a string or a list of strings depending on whether it has been pretokenized. If each sequence is provided as a list of strings (pretokenized), you must set
is_split_into_words
asTrue
to disambiguate with a sequence pair.max_length (int, optional) – If set to a number, will limit the total sequence returned so that it has a maximum length. If there are overflowing tokens, those overflowing tokens will be added to the returned dictionary when
return_overflowing_tokens
isTrue
. Defaults toNone
.stride (int, optional) – Only available for batch input of sequence pair and mainly for question answering usage. When for QA,
text
represents questions andtext_pair
represents contexts. Ifstride
is set to a positive number, the context will be split into multiple spans wherestride
defines the number of (tokenized) tokens to skip from the start of one span to get the next span, thus will produce a bigger batch than inputs to include all spans. Moreover, ‘overflow_to_sample’ and ‘offset_mapping’ preserving the original example and position information will be added to the returned dictionary. Defaults to 0.padding (bool, optional) – If set to
True
, the returned sequences would be padded up tomax_length
specified length according to padding side (self.padding_side
) and padding token id. Defaults toFalse
.truncation_strategy (str, optional) – String selected in the following options: - ‘longest_first’ (default) Iteratively reduce the inputs sequence until the input is under
max_length
starting from the longest one at each token (when there is a pair of input sequences). - ‘only_first’: Only truncate the first sequence. - ‘only_second’: Only truncate the second sequence. - ‘do_not_truncate’: Do not truncate (raise an error if the input sequence is longer thanmax_length
). Defaults to ‘longest_first’.return_position_ids (bool, optional) – Whether to include tokens position ids in the returned dictionary. Defaults to
False
.return_token_type_ids (bool, optional) – Whether to include token type ids in the returned dictionary. Defaults to
True
.return_attention_mask (bool, optional) – Whether to include the attention mask in the returned dictionary. Defaults to
False
.return_length (bool, optional) – Whether to include the length of each encoded inputs in the returned dictionary. Defaults to
False
.return_overflowing_tokens (bool, optional) – Whether to include overflowing token information in the returned dictionary. Defaults to
False
.return_special_tokens_mask (bool, optional) – Whether to include special tokens mask information in the returned dictionary. Defaults to
False
.
- Returns
The dict has the following optional items: - input_ids (list[int]): List of token ids to be fed to a model. - position_ids (list[int], optional): List of token position ids to be
fed to a model. Included when
return_position_ids
isTrue
token_type_ids (list[int], optional): List of token type ids to be fed to a model. Included when
return_token_type_ids
isTrue
.attention_mask (list[int], optional): List of integers valued 0 or 1, where 0 specifies paddings and should not be attended to by the model. Included when
return_attention_mask
isTrue
.seq_len (int, optional): The input_ids length. Included when
return_length
isTrue
.overflowing_tokens (list[int], optional): List of overflowing tokens. Included when if
max_length
is specified andreturn_overflowing_tokens
is True.num_truncated_tokens (int, optional): The number of overflowing tokens. Included when if
max_length
is specified andreturn_overflowing_tokens
is True.special_tokens_mask (list[int], optional): List of integers valued 0 or 1, with 0 specifying special added tokens and 1 specifying sequence tokens. Included when
return_special_tokens_mask
isTrue
.offset_mapping (list[int], optional): list of pair preserving the index of start and end char in original input for each token. For a sqecial token, the index pair is
(0, 0)
. Included whenstride
works.overflow_to_sample (int, optional): Index of example from which this feature is generated. Included when
stride
works.
- Return type
dict
-