Holds the output of the :meth:`~transformers.tokenization_utils_base.PreTrainedTokenizerBase.encode_plus` and :meth:`~transformers.tokenization_utils_base.PreTrainedTokenizerBase.batch_encode` methods (tokens, attention_masks, etc). This class is derived from a python dictionary an
| 154 | |
| 155 | |
| 156 | class BatchEncoding(UserDict): |
| 157 | """ |
| 158 | Holds the output of the :meth:`~transformers.tokenization_utils_base.PreTrainedTokenizerBase.encode_plus` and |
| 159 | :meth:`~transformers.tokenization_utils_base.PreTrainedTokenizerBase.batch_encode` methods (tokens, |
| 160 | attention_masks, etc). |
| 161 | |
| 162 | This class is derived from a python dictionary and can be used as a dictionary. In addition, this class exposes |
| 163 | utility methods to map from word/character space to token space. |
| 164 | |
| 165 | Args: |
| 166 | data (:obj:`dict`): |
| 167 | Dictionary of lists/arrays/tensors returned by the encode/batch_encode methods ('input_ids', |
| 168 | 'attention_mask', etc.). |
| 169 | encoding (:obj:`tokenizers.Encoding` or :obj:`Sequence[tokenizers.Encoding]`, `optional`): |
| 170 | If the tokenizer is a fast tokenizer which outputs additional information like mapping from word/character |
| 171 | space to token space the :obj:`tokenizers.Encoding` instance or list of instance (for batches) hold this |
| 172 | information. |
| 173 | tensor_type (:obj:`Union[None, str, TensorType]`, `optional`): |
| 174 | You can give a tensor_type here to convert the lists of integers in PyTorch/TensorFlow/Numpy Tensors at |
| 175 | initialization. |
| 176 | prepend_batch_axis (:obj:`bool`, `optional`, defaults to :obj:`False`): |
| 177 | Whether or not to add a batch axis when converting to tensors (see :obj:`tensor_type` above). |
| 178 | n_sequences (:obj:`Optional[int]`, `optional`): |
| 179 | You can give a tensor_type here to convert the lists of integers in PyTorch/TensorFlow/Numpy Tensors at |
| 180 | initialization. |
| 181 | """ |
| 182 | |
| 183 | def __init__( |
| 184 | self, |
| 185 | data: Optional[Dict[str, Any]] = None, |
| 186 | encoding: Optional[Union[EncodingFast, Sequence[EncodingFast]]] = None, |
| 187 | tensor_type: Union[None, str, TensorType] = None, |
| 188 | prepend_batch_axis: bool = False, |
| 189 | n_sequences: Optional[int] = None, |
| 190 | ): |
| 191 | super().__init__(data) |
| 192 | |
| 193 | if isinstance(encoding, EncodingFast): |
| 194 | encoding = [encoding] |
| 195 | |
| 196 | self._encodings = encoding |
| 197 | |
| 198 | if n_sequences is None and encoding is not None and len(encoding): |
| 199 | n_sequences = encoding[0].n_sequences |
| 200 | |
| 201 | self._n_sequences = n_sequences |
| 202 | |
| 203 | self.convert_to_tensors(tensor_type=tensor_type, prepend_batch_axis=prepend_batch_axis) |
| 204 | |
| 205 | @property |
| 206 | def n_sequences(self) -> Optional[int]: |
| 207 | """ |
| 208 | :obj:`Optional[int]`: The number of sequences used to generate each sample from the batch encoded in this |
| 209 | :class:`~transformers.BatchEncoding`. Currently can be one of :obj:`None` (unknown), :obj:`1` (a single |
| 210 | sentence) or :obj:`2` (a pair of sentences) |
| 211 | """ |
| 212 | return self._n_sequences |
| 213 |
no outgoing calls
searching dependent graphs…