# Data Collator

## 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](https://huggingface.co/docs/transformers/v4.34.1/en/main_classes/data_collator#transformers.DataCollatorForLanguageModeling)) also apply some random data augmentation (like random masking) on the formed batch.

Examples of use can be found in the [example scripts](https://huggingface.co/docs/transformers/examples) or [example notebooks](https://huggingface.co/docs/transformers/notebooks).

### Default data collator

**transformers.default\_data\_collator**

[\<source>](https://github.com/huggingface/transformers/blob/v4.34.1/src/transformers/data/data_collator.py#L52)

( features: typing.List\[InputDataClass]return\_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>](https://github.com/huggingface/transformers/blob/v4.34.1/src/transformers/data/data_collator.py#L78)

( return\_tensors: str = 'pt' )

Parameters

* **return\_tensors** (`str`) — The type of Tensor to return. Allowable values are “np”, “pt” and “tf”.

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>](https://github.com/huggingface/transformers/blob/v4.34.1/src/transformers/data/data_collator.py#L215)

( tokenizer: PreTrainedTokenizerBasepadding: typing.Union\[bool, str, transformers.utils.generic.PaddingStrategy] = Truemax\_length: typing.Optional\[int] = Nonepad\_to\_multiple\_of: typing.Optional\[int] = Nonereturn\_tensors: str = 'pt' )

Parameters

* **tokenizer** ([PreTrainedTokenizer](https://huggingface.co/docs/transformers/v4.34.1/en/main_classes/tokenizer#transformers.PreTrainedTokenizer) or [PreTrainedTokenizerFast](https://huggingface.co/docs/transformers/v4.34.1/en/main_classes/tokenizer#transformers.PreTrainedTokenizerFast)) — The tokenizer used for encoding the data.
* **padding** (`bool`, `str` or [PaddingStrategy](https://huggingface.co/docs/transformers/v4.34.1/en/internal/file_utils#transformers.utils.PaddingStrategy), *optional*, defaults to `True`) — Select a strategy to pad the returned sequences (according to the model’s 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.5 (Volta).
* **return\_tensors** (`str`) — The type of Tensor to return. Allowable values are “np”, “pt” and “tf”.

Data collator that will dynamically pad the inputs received.

### DataCollatorForTokenClassification

#### class transformers.DataCollatorForTokenClassification

[\<source>](https://github.com/huggingface/transformers/blob/v4.34.1/src/transformers/data/data_collator.py#L266)

( tokenizer: PreTrainedTokenizerBasepadding: typing.Union\[bool, str, transformers.utils.generic.PaddingStrategy] = Truemax\_length: typing.Optional\[int] = Nonepad\_to\_multiple\_of: typing.Optional\[int] = Nonelabel\_pad\_token\_id: int = -100return\_tensors: str = 'pt' )

Parameters

* **tokenizer** ([PreTrainedTokenizer](https://huggingface.co/docs/transformers/v4.34.1/en/main_classes/tokenizer#transformers.PreTrainedTokenizer) or [PreTrainedTokenizerFast](https://huggingface.co/docs/transformers/v4.34.1/en/main_classes/tokenizer#transformers.PreTrainedTokenizerFast)) — The tokenizer used for encoding the data.
* **padding** (`bool`, `str` or [PaddingStrategy](https://huggingface.co/docs/transformers/v4.34.1/en/internal/file_utils#transformers.utils.PaddingStrategy), *optional*, defaults to `True`) — Select a strategy to pad the returned sequences (according to the model’s 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.5 (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`) — The type of Tensor to return. Allowable values are “np”, “pt” and “tf”.

Data collator that will dynamically pad the inputs received, as well as the labels.

### DataCollatorForSeq2Seq

#### class transformers.DataCollatorForSeq2Seq

[\<source>](https://github.com/huggingface/transformers/blob/v4.34.1/src/transformers/data/data_collator.py#L517)

( tokenizer: PreTrainedTokenizerBasemodel: typing.Optional\[typing.Any] = Nonepadding: typing.Union\[bool, str, transformers.utils.generic.PaddingStrategy] = Truemax\_length: typing.Optional\[int] = Nonepad\_to\_multiple\_of: typing.Optional\[int] = Nonelabel\_pad\_token\_id: int = -100return\_tensors: str = 'pt' )

Parameters

* **tokenizer** ([PreTrainedTokenizer](https://huggingface.co/docs/transformers/v4.34.1/en/main_classes/tokenizer#transformers.PreTrainedTokenizer) or [PreTrainedTokenizerFast](https://huggingface.co/docs/transformers/v4.34.1/en/main_classes/tokenizer#transformers.PreTrainedTokenizerFast)) — The tokenizer used for encoding the data.
* **model** ([PreTrainedModel](https://huggingface.co/docs/transformers/v4.34.1/en/main_classes/model#transformers.PreTrainedModel)) — 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](https://huggingface.co/docs/transformers/v4.34.1/en/internal/file_utils#transformers.utils.PaddingStrategy), *optional*, defaults to `True`) — Select a strategy to pad the returned sequences (according to the model’s 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.5 (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`) — The type of Tensor to return. Allowable values are “np”, “pt” and “tf”.

Data collator that will dynamically pad the inputs received, as well as the labels.

### DataCollatorForLanguageModeling

#### class transformers.DataCollatorForLanguageModeling

[\<source>](https://github.com/huggingface/transformers/blob/v4.34.1/src/transformers/data/data_collator.py#L607)

( tokenizer: PreTrainedTokenizerBasemlm: bool = Truemlm\_probability: float = 0.15pad\_to\_multiple\_of: typing.Optional\[int] = Nonetf\_experimental\_compile: bool = Falsereturn\_tensors: str = 'pt' )

Parameters

* **tokenizer** ([PreTrainedTokenizer](https://huggingface.co/docs/transformers/v4.34.1/en/main_classes/tokenizer#transformers.PreTrainedTokenizer) or [PreTrainedTokenizerFast](https://huggingface.co/docs/transformers/v4.34.1/en/main_classes/tokenizer#transformers.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.
* **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`.
* **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”, “pt” and “tf”.

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](https://huggingface.co/docs/transformers/v4.34.1/en/main_classes/tokenizer#transformers.PreTrainedTokenizer) or a [PreTrainedTokenizerFast](https://huggingface.co/docs/transformers/v4.34.1/en/main_classes/tokenizer#transformers.PreTrainedTokenizerFast) with the argument `return_special_tokens_mask=True`.

**numpy\_mask\_tokens**

[\<source>](https://github.com/huggingface/transformers/blob/v4.34.1/src/transformers/data/data_collator.py#L806)

( inputs: typing.Anyspecial\_tokens\_mask: typing.Optional\[typing.Any] = None )

Prepare masked tokens inputs/labels for masked language modeling: 80% MASK, 10% random, 10% original.

**tf\_mask\_tokens**

[\<source>](https://github.com/huggingface/transformers/blob/v4.34.1/src/transformers/data/data_collator.py#L659)

( inputs: typing.Anyvocab\_sizemask\_token\_idspecial\_tokens\_mask: typing.Optional\[typing.Any] = None )

Prepare masked tokens inputs/labels for masked language modeling: 80% MASK, 10% random, 10% original.

**torch\_mask\_tokens**

[\<source>](https://github.com/huggingface/transformers/blob/v4.34.1/src/transformers/data/data_collator.py#L751)

( inputs: typing.Anyspecial\_tokens\_mask: typing.Optional\[typing.Any] = None )

Prepare masked tokens inputs/labels for masked language modeling: 80% MASK, 10% random, 10% original.

### DataCollatorForWholeWordMask

#### class transformers.DataCollatorForWholeWordMask

[\<source>](https://github.com/huggingface/transformers/blob/v4.34.1/src/transformers/data/data_collator.py#L845)

( tokenizer: PreTrainedTokenizerBasemlm: bool = Truemlm\_probability: float = 0.15pad\_to\_multiple\_of: typing.Optional\[int] = Nonetf\_experimental\_compile: bool = Falsereturn\_tensors: str = 'pt' )

Data collator used for language modeling that masks entire words.

* collates batches of tensors, honoring their tokenizer’s pad\_token
* preprocesses batches for masked language modeling

This collator relies on details of the implementation of subword tokenization by [BertTokenizer](https://huggingface.co/docs/transformers/v4.34.1/en/model_doc/bert#transformers.BertTokenizer), specifically that subword tokens are prefixed with *##*. For tokenizers that do not adhere to this scheme, this collator will produce an output that is roughly equivalent to `.DataCollatorForLanguageModeling`.

**numpy\_mask\_tokens**

[\<source>](https://github.com/huggingface/transformers/blob/v4.34.1/src/transformers/data/data_collator.py#L1075)

( inputs: typing.Anymask\_labels: typing.Any )

Prepare masked tokens inputs/labels for masked language modeling: 80% MASK, 10% random, 10% original. Set ‘mask\_labels’ means we use whole word mask (wwm), we directly mask idxs according to it’s ref.

**tf\_mask\_tokens**

[\<source>](https://github.com/huggingface/transformers/blob/v4.34.1/src/transformers/data/data_collator.py#L1033)

( inputs: typing.Anymask\_labels: typing.Any )

Prepare masked tokens inputs/labels for masked language modeling: 80% MASK, 10% random, 10% original. Set ‘mask\_labels’ means we use whole word mask (wwm), we directly mask idxs according to it’s ref.

**torch\_mask\_tokens**

[\<source>](https://github.com/huggingface/transformers/blob/v4.34.1/src/transformers/data/data_collator.py#L993)

( inputs: typing.Anymask\_labels: typing.Any )

Prepare masked tokens inputs/labels for masked language modeling: 80% MASK, 10% random, 10% original. Set ‘mask\_labels’ means we use whole word mask (wwm), we directly mask idxs according to it’s ref.

### DataCollatorForPermutationLanguageModeling

#### class transformers.DataCollatorForPermutationLanguageModeling

[\<source>](https://github.com/huggingface/transformers/blob/v4.34.1/src/transformers/data/data_collator.py#L1200)

( tokenizer: PreTrainedTokenizerBaseplm\_probability: float = 0.16666666666666666max\_span\_length: int = 5return\_tensors: str = 'pt' )

Data collator used for permutation language modeling.

* collates batches of tensors, honoring their tokenizer’s pad\_token
* preprocesses batches for permutation language modeling with procedures specific to XLNet

**numpy\_mask\_tokens**

[\<source>](https://github.com/huggingface/transformers/blob/v4.34.1/src/transformers/data/data_collator.py#L1440)

( inputs: typing.Any )

The masked tokens to be predicted for a particular sequence are determined by the following algorithm:

1. Start from the beginning of the sequence by setting `cur_len = 0` (number of tokens processed so far).
2. Sample a `span_length` from the interval `[1, max_span_length]` (length of span of tokens to be masked)
3. Reserve a context of length `context_length = span_length / plm_probability` to surround span to be masked
4. Sample a starting point `start_index` from the interval `[cur_len, cur_len + context_length - span_length]` and mask tokens `start_index:start_index + span_length`
5. Set `cur_len = cur_len + context_length`. If `cur_len < max_len` (i.e. there are tokens remaining in the sequence to be processed), repeat from Step 1.

**tf\_mask\_tokens**

[\<source>](https://github.com/huggingface/transformers/blob/v4.34.1/src/transformers/data/data_collator.py#L1333)

( inputs: typing.Any )

The masked tokens to be predicted for a particular sequence are determined by the following algorithm:

1. Start from the beginning of the sequence by setting `cur_len = 0` (number of tokens processed so far).
2. Sample a `span_length` from the interval `[1, max_span_length]` (length of span of tokens to be masked)
3. Reserve a context of length `context_length = span_length / plm_probability` to surround span to be masked
4. Sample a starting point `start_index` from the interval `[cur_len, cur_len + context_length - span_length]` and mask tokens `start_index:start_index + span_length`
5. Set `cur_len = cur_len + context_length`. If `cur_len < max_len` (i.e. there are tokens remaining in the sequence to be processed), repeat from Step 1.

**torch\_mask\_tokens**

[\<source>](https://github.com/huggingface/transformers/blob/v4.34.1/src/transformers/data/data_collator.py#L1234)

( inputs: typing.Any )

The masked tokens to be predicted for a particular sequence are determined by the following algorithm:

1. Start from the beginning of the sequence by setting `cur_len = 0` (number of tokens processed so far).
2. Sample a `span_length` from the interval `[1, max_span_length]` (length of span of tokens to be masked)
3. Reserve a context of length `context_length = span_length / plm_probability` to surround span to be masked
4. Sample a starting point `start_index` from the interval `[cur_len, cur_len + context_length - span_length]` and mask tokens `start_index:start_index + span_length`
5. Set `cur_len = cur_len + context_length`. If `cur_len < max_len` (i.e. there are tokens remaining in the sequence to be processed), repeat from Step 1.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://boinc-ai.gitbook.io/transformers/api/main-classes/data-collator.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
