Qortora · Search · Indexed page

huggingface.coFetched 2026-08-17T11:01:59Z

Data Collator · Hugging Face

We’re on a journey to advance and democratize artificial intelligence through open source and open science.

Open original source · Full cached text

Data Collator · Hugging Face Transformers documentation Data Collator Transformers 🏡 View all docsAWS Trainium & InferentiaAccelerateArgillaAutoTrainBitsandbytesCLIChat UIDataset viewerDatasetsDeploying on AWSDiffusersDistilabelEvaluateGoogle CloudGoogle TPUsGradioHubHub Python LibraryHuggingface.jsInference Endpoints (dedicated)Inference ProvidersKernelsLeRobotLeaderboardsLightevalMicrosoft AzureOpenEnvOptimumPEFTReachy MiniSafetensorsSentence TransformersTRLTasksText Embeddings InferenceText Generation InferenceTokenizersTrackioTransformersTransformers.jsXetsmolagentstimm Search documentation mainv5.14.0v5.13.1v5.12.0v5.11.0v5.10.4v5.9.0v5.8.1v5.7.0v5.6.2v5.5.4v5.4.0v5.3.0v5.2.0v5.1.0v5.0.0v4.57.6v4.56.2v4.55.4v4.53.3v4.52.3v4.51.3v4.50.0v4.49.0v4.48.2v4.47.1v4.46.3v4.45.2v4.44.2v4.43.4v4.42.4v4.41.2v4.40.2v4.39.3v4.38.2v4.37.2v4.36.1v4.35.2v4.34.1v4.33.3v4.32.1v4.31.0v4.30.0v4.29.1v4.28.1v4.27.2v4.26.1v4.25.1v4.24.0v4.23.1v4.22.2v4.21.3v4.20.1v4.19.4v4.18.0v4.17.0v4.16.2v4.15.0v4.14.1v4.13.0v4.12.5v4.11.3v4.10.1v4.9.2v4.8.2v4.7.0v4.6.0v4.5.1v4.4.2v4.3.3v4.2.2v4.1.1v4.0.1v3.5.1v3.4.0v3.3.1v3.2.0v3.1.0v3.0.2v2.11.0v2.10.0v2.9.1v2.8.0v2.7.0v2.6.0v2.5.1v2.4.1v2.3.0v2.2.2v2.1.1v2.0.0v1.2.0v1.1.0v1.0.0doc-builder-html ARDEENESFRHIITJAKOPTROTRZH Join the Hugging Face community and get access to the augmented documentation experience Collaborate on models, datasets and Spaces Faster examples with accelerated inference Switch between documentation themes Sign Up to get started Copy page Data Collator Data collators are objects that will form a batch by using a list of dataset elements as input. These elements are of the same type as the elements of train_dataset or eval_dataset. To be able to build batches, data collators may apply some processing (like padding). Some of them (like DataCollatorForLanguageModeling) also apply some random data augmentation (like random masking) on the formed batch. Examples of use can be found in the example scripts or example notebooks. Default data collator transformers.default_data_collator < source > ( features: listreturn_tensors = 'pt' ) Very simple data collator that simply collates batches of dict-like objects and performs special handling for potential keys named: label: handles a single value (int or float) per object label_ids: handles a list of values per object Does not do any additional preprocessing: property names of the input object will be used as corresponding inputs to the model. See glue and ner for example of how it’s useful. DefaultDataCollator class transformers.DefaultDataCollator < source > ( return_tensors: str = 'pt' ) Parameters return_tensors (str, optional, defaults to "pt") The type of Tensor to return. Allowable values are np, or pt. Very simple data collator that simply collates batches of dict-like objects and performs special handling for potential keys named: label: handles a single value (int or float) per object label_ids: handles a list of values per object Does not do any additional preprocessing: property names of the input object will be used as corresponding inputs to the model. See glue and ner for example of how it’s useful. This is an object (like other data collators) rather than a pure function like default_data_collator. This can be helpful if you need to set a return_tensors value at initialization. DataCollatorWithPadding class transformers.DataCollatorWithPadding < source > ( tokenizer: PreTrainedTokenizerBasepadding: bool | str | transformers.utils.generic.PaddingStrategy = Truemax_length: int | None = Nonepad_to_multiple_of: int | None = Nonereturn_tensors: str = 'pt' ) Parameters tokenizer (PreTrainedTokenizer or PreTrainedTokenizerFast) The tokenizer used for encoding the data. padding (bool, str or PaddingStrategy, optional, defaults to True) Select a strategy to pad the returned sequences (according to the models padding side and padding index) among: True or 'longest' (default): Pad to the longest sequence in the batch (or no padding if only a single sequence is provided). 'max_length': Pad to a maximum length specified with the argument max_length or to the maximum acceptable input length for the model if that argument is not provided. False or 'do_not_pad': No padding (i.e., can output a batch with sequences of different lengths). max_length (int, optional) Maximum length of the returned list and optionally padding length (see above). pad_to_multiple_of (int, optional) If set will pad the sequence to a multiple of the provided value. This is especially useful to enable the use of Tensor Cores on NVIDIA hardware with compute capability >= 7.0 (Volta). return_tensors (str, optional, defaults to "pt") The type of Tensor to return. Allowable values are np, or pt. Data collator that will dynamically pad the inputs received. DataCollatorForTokenClassification class transformers.DataCollatorForTokenClassification < source > ( tokenizer: PreTrainedTokenizerBasepadding: bool | str | transformers.utils.generic.PaddingStrategy = Truemax_length: int | None = Nonepad_to_multiple_of: int | None = Nonelabel_pad_token_id: int = -100return_tensors: str = 'pt' ) Parameters tokenizer (PreTrainedTokenizer or PreTrainedTokenizerFast) The tokenizer used for encoding the data. padding (bool, str or PaddingStrategy, optional, defaults to True) Select a strategy to pad the returned sequences (according to the models padding side and padding index) among: True or 'longest' (default): Pad to the longest sequence in the batch (or no padding if only a single sequence is provided). 'max_length': Pad to a maximum length specified with the argument max_length or to the maximum acceptable input length for the model if that argument is not provided. False or 'do_not_pad': No padding (i.e., can output a batch with sequences of different lengths). max_length (int, optional) Maximum length of the returned list and optionally padding length (see above). pad_to_multiple_of (int, optional) If set will pad the sequence to a multiple of the provided value. This is especially useful to enable the use of Tensor Cores on NVIDIA hardware with compute capability >= 7.0 (Volta). label_pad_token_id (int, optional, defaults to -100) The id to use when padding the labels (-100 will be automatically ignore by PyTorch loss functions). return_tensors (str, optional, defaults to "pt") The type of Tensor to return. Allowable values are np, or pt. Data collator that will dynamically pad the inputs received, as well as the labels. DataCollatorForSeq2Seq class transformers.DataCollatorForSeq2Seq < source > ( tokenizer: PreTrainedTokenizerBasemodel: typing.Optional[typing.Any] = Nonepadding: bool | str | transformers.utils.generic.PaddingStrategy = Truemax_length: int | None = Nonepad_to_multiple_of: int | None = Nonelabel_pad_token_id: int = -100return_tensors: str = 'pt' ) Parameters tokenizer (PreTrainedTokenizer or PreTrainedTokenizerFast) The tokenizer used for encoding the data. model (PreTrainedModel, optional) The model that is being trained. If set and has the prepare_decoder_input_ids_from_labels, use it to prepare the decoder_input_ids This is useful when using label_smoothing to avoid calculating loss twice. padding (bool, str or PaddingStrategy, optional, defaults to True) Select a strategy to pad the returned sequences (according to the models padding side and padding index) among: True or 'longest' (default): Pad to the longest sequence in the batch (or no padding if only a single sequence is provided). 'max_length': Pad to a maximum length specified with the argument max_length or to the maximum acceptable input length for the model if that argument is not provided. False or 'do_not_pad': No padding (i.e., can output a batch with sequences of different lengths). max_length (int, optional) Maximum length of the returned list and optionally padding length (see above). pad_to_multiple_of (int, optional) If set will pad the sequence to a multiple of the provided value. This is especially useful to enable the use of Tensor Cores on NVIDIA hardware with compute capability >= 7.0 (Volta). label_pad_token_id (int, optional, defaults to -100) The id to use when padding the labels (-100 will be automatically ignored by PyTorch loss functions). return_tensors (str, optional, defaults to "pt") The type of Tensor to return. Allowable values are np, or pt. Data collator that will dynamically pad the inputs received, as well as the labels. DataCollatorForLanguageModeling class transformers.DataCollatorForLanguageModeling < source > ( tokenizer: PreTrainedTokenizerBasemlm: bool = Truewhole_word_mask: bool = Falsemlm_probability: float | None = 0.15mask_replace_prob: float = 0.8random_replace_prob: float = 0.1pad_to_multiple_of: int | None = Nonereturn_tensors: str = 'pt'seed: int | None = None ) Parameters tokenizer (PreTrainedTokenizer or PreTrainedTokenizerFast) The tokenizer used for encoding the data. mlm (bool, optional, defaults to True) Whether or not to use masked language modeling. If set to False, the labels are the same as the inputs with the padding tokens ignored (by setting them to -100). Otherwise, the labels are -100 for non-masked tokens and the value to predict for the masked token. whole_word_mask (bool, optional, defaults to False) Whether or not to mask whole words instead of individual tokens. mlm_probability (float, optional, defaults to 0.15) The probability with which to (randomly) mask tokens in the input, when mlm is set to True. mask_replace_prob (float, optional, defaults to 0.8) The probability with which masked tokens are replaced by the tokenizers mask token (e.g., [MASK]). Defaults to 0.8, meaning 80% of the masked tokens will be replaced with [MASK]. Only works when mlm is set to True. random_replace_prob (float, optional, defaults to 0.1) The probability with which masked tokens are replaced by random tokens from the tokenizers vocabulary. Defaults to 0.1, meaning 10% of the masked tokens will be replaced with random tokens. The remaining masked tokens (1 - mask_replace_prob - random_replace_prob) are left unchanged. Only works when mlm is set to True. pad_to_multiple_of (int, optional) If set, will pad the sequence to a multiple of the provided value. return_tensors (str) The type of Tensor to return. Allowable values are np, or pt. seed (int, optional) The seed to use for the random number generator for masking. If not provided, the global RNG will be used. Data collator used for language modeling. Inputs are dynamically padded to the maximum length of a batch if they are not all of the same length. For best performance, this data collator should be used with a dataset having items that are dictionaries or BatchEncoding, with the "special_tokens_mask" key, as returned by a PreTrainedTokenizer or a PreTrainedTokenizerFast with the argument return_special_tokens_mask=True. <Example Options and Expectations> Default Behavior: mask_replace_prob=0.8, random_replace_prob=0.1. Expect 80% of masked tokens replaced with [MASK], 10% replaced with random tokens, and 10% left unchanged. All masked tokens replaced by [MASK]: mask_replace_prob=1.0, random_replace_prob=0.0. Expect all masked tokens to be replaced with [MASK]. No tokens are left unchanged or replaced with random tokens. No [MASK] replacement, only random tokens: mask_replace_prob=0.0, random_replace_prob=1.0. Expect all masked tokens to be replaced with random tokens. No [MASK] replacements or unchanged tokens. Balanced replacement: mask_replace_prob=0.5, random_replace_prob=0.4. Expect 50% of masked tokens replaced with [MASK], 40% replaced with random tokens, and 10% left unchanged. Note: The sum of mask_replace_prob and random_replace_prob must not exceed 1. If their sum is less than 1, the remaining proportion will consist of masked tokens left unchanged. numpy_mask_tokens < source > ( inputs: typing.Anyspecial_tokens_mask: typing.Optional[typing.Any] = Noneoffset_mapping: typing.Optional[typing.Any] = None ) Prepare masked tokens inputs/label…