tokenizer#

class DebertaV2Tokenizer(sentencepiece_model_file, vocab_file=None, do_lower_case=False, split_by_punct=False, bos_token='[CLS]', eos_token='[SEP]', unk_token='[UNK]', sep_token='[SEP]', pad_token='[PAD]', cls_token='[CLS]', mask_token='[MASK]', sp_model_kwargs: Dict[str, Any] | None = None, **kwargs)[源代码]#

基类:PretrainedTokenizer

Constructs a DeBERTa-v2 tokenizer. Based on [SentencePiece](google/sentencepiece).

参数:
  • vocab_file (str) -- [SentencePiece](google/sentencepiece) file (generally has a .spm extension) that contains the vocabulary necessary to instantiate a tokenizer.

  • do_lower_case (bool, optional, defaults to False) -- Whether or not to lowercase the input when tokenizing.

  • bos_token (string, optional, defaults to "[CLS]") -- The beginning of sequence token that was used during pre-training. Can be used a sequence classifier token. When building a sequence using special tokens, this is not the token that is used for the beginning of sequence. The token used is the cls_token.

  • eos_token (string, optional, defaults to "[SEP]") -- The end of sequence token. When building a sequence using special tokens, this is not the token that is used for the end of sequence. The token used is the sep_token.

  • unk_token (str, optional, defaults to "[UNK]") -- The unknown token. A token that is not in the vocabulary cannot be converted to an ID and is set to be this token instead.

  • sep_token (str, optional, defaults to "[SEP]") -- The separator token, which is used when building a sequence from multiple sequences, e.g. two sequences for sequence classification or for a text and a question for question answering. It is also used as the last token of a sequence built with special tokens.

  • pad_token (str, optional, defaults to "[PAD]") -- The token used for padding, for example when batching sequences of different lengths.

  • cls_token (str, optional, defaults to "[CLS]") -- The classifier token which is used when doing sequence classification (classification of the whole sequence instead of per-token classification). It is the first token of the sequence when built with special tokens.

  • mask_token (str, optional, defaults to "[MASK]") -- The token used for masking values. This is the token used when training this model with masked language modeling. This is the token which the model will try to predict.

  • sp_model_kwargs (dict, optional) --

    Will be passed to the SentencePieceProcessor.__init__() method. The [Python wrapper for SentencePiece](google/sentencepiece) can be used, among other things, to set:

    • enable_sampling: Enable subword regularization.

    • nbest_size: Sampling parameters for unigram. Invalid for BPE-Dropout.

      • nbest_size = {0,1}: No sampling is performed.

      • nbest_size > 1: samples from the nbest_size results.

      • nbest_size < 0: assuming that nbest_size is infinite and samples from the all hypothesis (lattice) using forward-filtering-and-backward-sampling algorithm.

    • alpha: Smoothing parameter for unigram sampling, and dropout probability of merge operations for BPE-dropout.

property vocab_size#

Size of the base vocabulary (without the added tokens).

Type:

int

get_vocab()[源代码]#

Returns the vocabulary as a dictionary of token to index.

tokenizer.get_vocab()[token] is equivalent to tokenizer.convert_tokens_to_ids(token) when token is in the vocab.

返回:

The vocabulary.

返回类型:

Dict[str, int]

convert_tokens_to_string(tokens)[源代码]#

Converts a sequence of tokens (string) in a single string.

build_inputs_with_special_tokens(token_ids_0, token_ids_1=None)[源代码]#

Build model inputs from a sequence or a pair of sequence for sequence classification tasks by concatenating and adding special tokens. A DeBERTa sequence has the following format:

  • single sequence: [CLS] X [SEP]

  • pair of sequences: [CLS] A [SEP] B [SEP]

参数:
  • token_ids_0 (List[int]) -- List of IDs to which the special tokens will be added.

  • token_ids_1 (List[int], optional) -- Optional second list of IDs for sequence pairs.

返回:

List of [input IDs](../glossary#input-ids) with the appropriate special tokens.

返回类型:

List[int]

get_special_tokens_mask(token_ids_0, token_ids_1=None, already_has_special_tokens=False)[源代码]#

Retrieves sequence ids from a token list that has no special tokens added. This method is called when adding special tokens using the tokenizer prepare_for_model or encode_plus methods.

参数:
  • token_ids_0 (List[int]) -- List of IDs.

  • token_ids_1 (List[int], optional) -- Optional second list of IDs for sequence pairs.

  • already_has_special_tokens (bool, optional, defaults to False) -- Whether or not the token list is already formatted with special tokens for the model.

返回:

A list of integers in the range [0, 1]: 1 for a special token, 0 for a sequence token.

返回类型:

List[int]

create_token_type_ids_from_sequences(token_ids_0, token_ids_1=None)[源代码]#

Create a mask from the two sequences passed to be used in a sequence-pair classification task. A DeBERTa sequence pair mask has the following format:

` 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 | first sequence    | second sequence | `

If token_ids_1 is None, this method only returns the first portion of the mask (0s).

参数:
  • token_ids_0 (List[int]) -- List of IDs.

  • token_ids_1 (List[int], optional) -- Optional second list of IDs for sequence pairs.

返回:

List of [token type IDs](../glossary#token-type-ids) according to the given sequence(s).

返回类型:

List[int]

prepare_for_tokenization(text, is_split_into_words=False, **kwargs)[源代码]#

Performs any necessary transformations before tokenization.

This method should pop the arguments from kwargs and return the remaining kwargs as well. We test the kwargs at the end of the encoding process to be sure all the arguments have been used.

参数:
  • text (str) -- The text to prepare.

  • is_split_into_words (bool, optional, defaults to False) -- Whether or not the input is already pre-tokenized (e.g., split into words). If set to True, the tokenizer assumes the input is already split into words (for instance, by splitting it on whitespace) which it will tokenize. This is useful for NER or token classification.

  • kwargs -- Keyword arguments to use for the tokenization.

返回:

The prepared text and the unused kwargs.

返回类型:

Tuple[str, Dict[str, Any]]

save_vocabulary(save_directory: str, filename_prefix: str | None = None) Tuple[str][源代码]#

Save all tokens to a vocabulary file. The file contains a token per line, and the line number would be the index of corresponding token.

参数:
  • filepath (str) -- File path to be saved to.

  • vocab (Vocab|dict) -- The Vocab or dict instance to be saved.

build_offset_mapping_with_special_tokens(offset_mapping_0, offset_mapping_1=None)[源代码]#

Build offset map from a pair of offset map by concatenating and adding offsets of special tokens.

A BERT offset_mapping has the following format:

  • single sequence: (0,0) X (0,0)

  • pair of sequences: (0,0) A (0,0) B (0,0)

参数:
  • offset_mapping_ids_0 (List[tuple]) -- List of wordpiece offsets to which the special tokens will be added.

  • offset_mapping_ids_1 (List[tuple], optional) -- Optional second list of wordpiece offsets for offset mapping pairs. Defaults to None.

返回:

A list of wordpiece offsets with the appropriate offsets of special tokens.

返回类型:

List[tuple]

save_resources(save_directory)[源代码]#

Saves SentencePiece file (ends with '.spm') under save_directory.

参数:

save_directory (str) -- Directory to save files into.