preprocessing.rst 22.2 KB
Newer Older
Sylvain Gugger's avatar
Sylvain Gugger committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
Preprocessing data
==================

In this tutorial, we'll explore how to preprocess your data using 馃 Transformers. The main tool for this is what we

call a :doc:`tokenizer <main_classes/tokenizer>`. You can build one using the tokenizer class associated to the model
you would like to use, or directly with the :class:`~transformers.AutoTokenizer` class.

As we saw in the :doc:`quicktour </quicktour>`, the tokenizer will first split a given text in words (or part of words,
punctuation symbols, etc.) usually called `tokens`. Then it will convert those `tokens` into numbers, to be able to
build a tensor out of them and feed them to the model. It will also add any additional inputs the model might expect to
work properly.

.. note::

    If you plan on using a pretrained model, it's important to use the associated pretrained tokenizer: it will split
    the text you give it in tokens the same way for the pretraining corpus, and it will use the same correspondence
    token to index (that we usually call a `vocab`) as during pretraining.

To automatically download the vocab used during pretraining or fine-tuning a given model, you can use the 
:func:`~transformers.AutoTokenizer.from_pretrained` method:

Sylvain Gugger's avatar
Sylvain Gugger committed
23
.. code-block::
Sylvain Gugger's avatar
Sylvain Gugger committed
24
25
26
27
28
29
30
31
32
33

    from transformers import AutoTokenizer
    tokenizer = AutoTokenizer.from_pretrained('bert-base-cased')

Base use
~~~~~~~~

A :class:`~transformers.PreTrainedTokenizer` has many methods, but the only one you need to remember for preprocessing
is its ``__call__``: you just need to feed your sentence to your tokenizer object.

Sylvain Gugger's avatar
Sylvain Gugger committed
34
.. code-block::
Sylvain Gugger's avatar
Sylvain Gugger committed
35

Sylvain Gugger's avatar
Sylvain Gugger committed
36
37
    >>> encoded_input = tokenizer("Hello, I'm a single sentence!")
    >>> print(encoded_input)
Sylvain Gugger's avatar
Sylvain Gugger committed
38
39
40
41
    {'input_ids': [101, 138, 18696, 155, 1942, 3190, 1144, 1572, 13745, 1104, 159, 9664, 2107, 102], 
     'token_type_ids': [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
     'attention_mask': [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]}

Sylvain Gugger's avatar
Sylvain Gugger committed
42
This returns a dictionary string to list of ints.
Sylvain Gugger's avatar
Sylvain Gugger committed
43
44
45
46
47
48
The `input_ids <glossary.html#input-ids>`__ are the indices corresponding to each token in our sentence. We will see
below what the `attention_mask <glossary.html#attention-mask>`__ is used for and in
:ref:`the next section <sentence-pairs>` the goal of `token_type_ids <glossary.html#token-type-ids>`__.

The tokenizer can decode a list of token ids in a proper sentence:

Sylvain Gugger's avatar
Sylvain Gugger committed
49
.. code-block::
Sylvain Gugger's avatar
Sylvain Gugger committed
50

Sylvain Gugger's avatar
Sylvain Gugger committed
51
    >>> tokenizer.decode(encoded_input["input_ids"])
Sylvain Gugger's avatar
Sylvain Gugger committed
52
53
54
55
56
57
58
59
60
61
    "[CLS] Hello, I'm a single sentence! [SEP]"

As you can see, the tokenizer automatically added some special tokens that the model expect. Not all model need special
tokens; for instance, if we had used` gtp2-medium` instead of `bert-base-cased` to create our tokenizer, we would have
seen the same sentence as the original one here. You can disable this behavior (which is only advised if you have added
those special tokens yourself) by passing ``add_special_tokens=False``.

If you have several sentences you want to process, you can do this efficiently by sending them as a list to the
tokenizer:

Sylvain Gugger's avatar
Sylvain Gugger committed
62
.. code-block::
Sylvain Gugger's avatar
Sylvain Gugger committed
63

Sylvain Gugger's avatar
Sylvain Gugger committed
64
65
66
67
68
    >>> batch_sentences = ["Hello I'm a single sentence",
    ...                    "And another sentence",
    ...                    "And the very very last one"]
    >>> encoded_inputs = tokenizer(batch_sentences)
    >>> print(encoded_inputs)
Sylvain Gugger's avatar
Sylvain Gugger committed
69
70
71
72
73
74
75
76
77
78
    {'input_ids': [[101, 8667, 146, 112, 182, 170, 1423, 5650, 102],
                   [101, 1262, 1330, 5650, 102],
                   [101, 1262, 1103, 1304, 1304, 1314, 1141, 102]],
     'token_type_ids': [[0, 0, 0, 0, 0, 0, 0, 0, 0],
                        [0, 0, 0, 0, 0],
                        [0, 0, 0, 0, 0, 0, 0, 0]],
     'attention_mask': [[1, 1, 1, 1, 1, 1, 1, 1, 1],
                        [1, 1, 1, 1, 1],
                        [1, 1, 1, 1, 1, 1, 1, 1]]}

Sylvain Gugger's avatar
Sylvain Gugger committed
79
80
We get back a dictionary once again, this time with values being list of list of ints.

Sylvain Gugger's avatar
Sylvain Gugger committed
81
82
83
84
85
86
87
88
89
If the purpose of sending several sentences at a time to the tokenizer is to build a batch to feed the model, you will
probably want:

- To pad each sentence to the maximum length there is in your batch.
- To truncate each sentence to the maximum length the model can accept (if applicable).
- To return tensors.

You can do all of this by using the following options when feeding your list of sentences to the tokenizer:

Sylvain Gugger's avatar
Sylvain Gugger committed
90
.. code-block::
Sylvain Gugger's avatar
Sylvain Gugger committed
91

Sylvain Gugger's avatar
Sylvain Gugger committed
92
93
94
    >>> ## PYTORCH CODE
    >>> batch = tokenizer(batch_sentences, padding=True, truncation=True, return_tensors="pt")
    >>> print(batch)
Sylvain Gugger's avatar
Sylvain Gugger committed
95
96
97
98
99
100
101
102
103
    {'input_ids': tensor([[ 101, 8667,  146,  112,  182,  170, 1423, 5650,  102],
                          [ 101, 1262, 1330, 5650,  102,    0,    0,    0,    0],
                          [ 101, 1262, 1103, 1304, 1304, 1314, 1141,  102,    0]]),
     'token_type_ids': tensor([[0, 0, 0, 0, 0, 0, 0, 0, 0],
                               [0, 0, 0, 0, 0, 0, 0, 0, 0],
                               [0, 0, 0, 0, 0, 0, 0, 0, 0]]), 
     'attention_mask': tensor([[1, 1, 1, 1, 1, 1, 1, 1, 1],
                               [1, 1, 1, 1, 1, 0, 0, 0, 0],
                               [1, 1, 1, 1, 1, 1, 1, 1, 0]])}
Sylvain Gugger's avatar
Sylvain Gugger committed
104
105
106
107
108
109
110
111
112
113
114
115
    >>> ## TENSORFLOW CODE
    >>> batch = tokenizer(batch_sentences, padding=True, truncation=True, return_tensors="tf")
    >>> print(batch)
    {'input_ids': tf.Tensor([[ 101, 8667,  146,  112,  182,  170, 1423, 5650,  102],
                          [ 101, 1262, 1330, 5650,  102,    0,    0,    0,    0],
                          [ 101, 1262, 1103, 1304, 1304, 1314, 1141,  102,    0]]),
     'token_type_ids': tf.Tensor([[0, 0, 0, 0, 0, 0, 0, 0, 0],
                               [0, 0, 0, 0, 0, 0, 0, 0, 0],
                               [0, 0, 0, 0, 0, 0, 0, 0, 0]]), 
     'attention_mask': tf.Tensor([[1, 1, 1, 1, 1, 1, 1, 1, 1],
                               [1, 1, 1, 1, 1, 0, 0, 0, 0],
                               [1, 1, 1, 1, 1, 1, 1, 1, 0]])}
Sylvain Gugger's avatar
Sylvain Gugger committed
116

Sylvain Gugger's avatar
Sylvain Gugger committed
117
118
119
It returns a dictionary string to tensor. We can now see what the `attention_mask <glossary.html#attention-mask>`__ is
all about: it points out which tokens the model should pay attention to and which ones it should not (because they
represent padding in this case).
Sylvain Gugger's avatar
Sylvain Gugger committed
120
121
122
123
124
125
126
127
128
129
130
131


Note that if your model does not have a maximum length associated to it, the command above will throw a warning. You
can safely ignore it. You can also pass ``verbose=False`` to stop the tokenizer to throw those kinds of warnings.

.. _sentence-pairs:

Preprocessing pairs of sentences
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Sometimes you need to feed pair of sentences to your model. For instance, if you want to classify if two sentences in a
pair are similar, or for question-answering models, which take a context and a question. For BERT models, the input is
Sylvain Gugger's avatar
Sylvain Gugger committed
132
then represented like this: :obj:`[CLS] Sequence A [SEP] Sequence B [SEP]`
Sylvain Gugger's avatar
Sylvain Gugger committed
133
134
135
136
137

You can encode a pair of sentences in the format expected by your model by supplying the two sentences as two arguments
(not a list since a list of two sentences will be interpreted as a batch of two single sentences, as we saw before).
This will once again return a dict string to list of ints:

Sylvain Gugger's avatar
Sylvain Gugger committed
138
.. code-block::
Sylvain Gugger's avatar
Sylvain Gugger committed
139

Sylvain Gugger's avatar
Sylvain Gugger committed
140
141
    >>> encoded_input = tokenizer("How old are you?", "I'm 6 years old")
    >>> print(encoded_input)
Sylvain Gugger's avatar
Sylvain Gugger committed
142
143
144
145
146
147
148
149
150
151
152
153
    {'input_ids': [101, 1731, 1385, 1132, 1128, 136, 102, 146, 112, 182, 127, 1201, 1385, 102], 
     'token_type_ids': [0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1], 
     'attention_mask': [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]}

This shows us what the `token_type_ids <glossary.html#token-type-ids>`__ are for: they indicate to the model which part
of the inputs correspond to the first sentence and which part corresponds to the second sentence. Note that
`token_type_ids` are not required or handled by all models. By default, a tokenizer will only return the inputs that
its associated model expects. You can force the return (or the non-return) of any of those special arguments by
using ``return_input_ids`` or ``return_token_type_ids``.

If we decode the token ids we obtained, we will see that the special tokens have been properly added.

Sylvain Gugger's avatar
Sylvain Gugger committed
154
.. code-block::
Sylvain Gugger's avatar
Sylvain Gugger committed
155

Sylvain Gugger's avatar
Sylvain Gugger committed
156
    >>> tokenizer.decode(encoded_input["input_ids"])
Sylvain Gugger's avatar
Sylvain Gugger committed
157
158
159
160
161
    "[CLS] How old are you? [SEP] I'm 6 years old [SEP]"

If you have a list of pairs of sequences you want to process, you should feed them as two lists to your tokenizer: the
list of first sentences and the list of second sentences:

Sylvain Gugger's avatar
Sylvain Gugger committed
162
.. code-block::
Sylvain Gugger's avatar
Sylvain Gugger committed
163

Sylvain Gugger's avatar
Sylvain Gugger committed
164
165
166
167
168
169
170
171
    >>> batch_sentences = ["Hello I'm a single sentence",
    ...                    "And another sentence",
    ...                    "And the very very last one"]
    >>> batch_of_second_sentences = ["I'm a sentence that goes with the first sentence",
    ...                              "And I should be encoded with the second sentence",
    ...                              "And I go with the very last one"]
    >>> encoded_inputs = tokenizer(batch_sentences, batch_of_second_sentences)
    >>> print(encoded_inputs)
Sylvain Gugger's avatar
Sylvain Gugger committed
172
173
174
175
176
177
178
179
180
181
    {'input_ids': [[101, 8667, 146, 112, 182, 170, 1423, 5650, 102, 146, 112, 182, 170, 5650, 1115, 2947, 1114, 1103, 1148, 5650, 102], 
                   [101, 1262, 1330, 5650, 102, 1262, 146, 1431, 1129, 12544, 1114, 1103, 1248, 5650, 102], 
                   [101, 1262, 1103, 1304, 1304, 1314, 1141, 102, 1262, 146, 1301, 1114, 1103, 1304, 1314, 1141, 102]], 
    'token_type_ids': [[0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], 
                       [0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], 
                       [0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1]], 
    'attention_mask': [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], 
                       [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], 
                       [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]}

Sylvain Gugger's avatar
Sylvain Gugger committed
182
As we can see, it returns a dictionary with the values being list of lists of ints.
Sylvain Gugger's avatar
Sylvain Gugger committed
183

Sylvain Gugger's avatar
Sylvain Gugger committed
184
To double-check what is fed to the model, we can decode each list in `input_ids` one by one:
Sylvain Gugger's avatar
Sylvain Gugger committed
185

Sylvain Gugger's avatar
Sylvain Gugger committed
186
.. code-block::
Sylvain Gugger's avatar
Sylvain Gugger committed
187

Sylvain Gugger's avatar
Sylvain Gugger committed
188
189
    >>> for ids in encoded_inputs["input_ids"]:
    >>>     print(tokenizer.decode(ids))
Sylvain Gugger's avatar
Sylvain Gugger committed
190
191
192
193
194
195
196
    [CLS] Hello I'm a single sentence [SEP] I'm a sentence that goes with the first sentence [SEP]
    [CLS] And another sentence [SEP] And I should be encoded with the second sentence [SEP]
    [CLS] And the very very last one [SEP] And I go with the very last one [SEP]

Once again, you can automatically pad your inputs to the maximum sentence length in the batch, truncate to the maximum
length the model can accept and return tensors directly with the following:

Sylvain Gugger's avatar
Sylvain Gugger committed
197
.. code-block::
Sylvain Gugger's avatar
Sylvain Gugger committed
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286

    ## PYTORCH CODE
    batch = tokenizer(batch_sentences, batch_of_second_sentences, padding=True, truncation=True, return_tensors="pt")
    ## TENSORFLOW CODE
    batch = tokenizer(batch_sentences, batch_of_second_sentences, padding=True, truncation=True, return_tensors="tf")

Everything you always wanted to know about padding and truncation
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

We have seen the commands that will work for most cases (pad your batch to the length of the maximum sentence and

truncate to the maximum length the mode can accept). However, the API supports more strategies if you need them. The
three arguments you need to know for this are :obj:`padding`, :obj:`truncation` and :obj:`max_length`.

- :obj:`padding` controls the padding. It can be a boolean or a string which should be:

    - :obj:`True` or :obj:`'longest'` to pad to the longest sequence in the batch (doing no padding if you only provide
      a single sequence).
    - :obj:`'max_length'` to pad to a length specified by the :obj:`max_length` argument or the maximum length accepted
      by the model if no :obj:`max_length` is provided (``max_length=None``). If you only provide a single sequence,
      padding will still be applied to it. 
    - :obj:`False` or :obj:`'do_not_pad'` to not pad the sequences. As we have seen before, this is the default
      behavior.

- :obj:`truncation` controls the truncation. It can be a boolean or a string which should be:

    - :obj:`True` or :obj:`'only_first'` truncate to a maximum length specified by the :obj:`max_length` argument or
      the maximum length accepted by the model if no :obj:`max_length` is provided (``max_length=None``). This will
      only truncate the first sentence of a pair if a pair of sequence (or a batch of pairs of sequences) is provided.
    - :obj:`'only_second'` truncate to a maximum length specified by the :obj:`max_length` argument or the maximum
      length accepted by the model if no :obj:`max_length` is provided (``max_length=None``). This will only truncate
      the second sentence of a pair if a pair of sequence (or a batch of pairs of sequences) is provided.
    - :obj:`'longest_first'` truncate to a maximum length specified by the :obj:`max_length` argument or the maximum
      length accepted by the model if no :obj:`max_length` is provided (``max_length=None``). This will truncate token
      by token, removing a token from the longest sequence in the pair until the proper length is reached.
    - :obj:`False` or :obj:`'do_not_truncate'` to not truncate the sequences. As we have seen before, this is the
      default behavior.

- :obj:`max_length` to control the length of the padding/truncation. It can be an integer or :obj:`None`, in which case
  it will default to the maximum length the model can accept. If the model has no specific maximum input length,
  truncation/padding to :obj:`max_length` is deactivated.

Here is a table summarizing the recommend way to setup padding and truncation. If you use pair of inputs sequence in
any of the following examples, you can replace :obj:`truncation=True` by a :obj:`STRATEGY` selected in 
:obj:`['only_first', 'only_second', 'longest_first']`, i.e. :obj:`truncation='only_second'` or
:obj:`truncation= 'longest_first'` to control how both sequence in the pair are truncated as detailed before.

+--------------------------------------+-----------------------------------+---------------------------------------------------------------------------------------------+
| Truncation                           | Padding                           | Instruction                                                                                 |
+======================================+===================================+=============================================================================================+
| no truncation                        | no padding                        | :obj:`tokenizer(batch_sentences)`                                                           |
|                                      +-----------------------------------+---------------------------------------------------------------------------------------------+
|                                      | padding to max sequence in batch  | :obj:`tokenizer(batch_sentences, padding=True)` or                                          |
|                                      |                                   | :obj:`tokenizer(batch_sentences, padding='longest')`                                        |
|                                      +-----------------------------------+---------------------------------------------------------------------------------------------+
|                                      | padding to max model input length | :obj:`tokenizer(batch_sentences, padding='max_length')`                                     |
|                                      +-----------------------------------+---------------------------------------------------------------------------------------------+
|                                      | padding to specific length        | :obj:`tokenizer(batch_sentences, padding='max_length', max_length=42)`                      |
+--------------------------------------+-----------------------------------+---------------------------------------------------------------------------------------------+
| truncation to max model input length | no padding                        | :obj:`tokenizer(batch_sentences, truncation=True)` or                                       |
|                                      |                                   | :obj:`tokenizer(batch_sentences, truncation=STRATEGY)`                                      |
|                                      +-----------------------------------+---------------------------------------------------------------------------------------------+
|                                      | padding to max sequence in batch  | :obj:`tokenizer(batch_sentences, padding=True, truncation=True)` or                         |
|                                      |                                   | :obj:`tokenizer(batch_sentences, padding=True, truncation=STRATEGY)`                        |
|                                      +-----------------------------------+---------------------------------------------------------------------------------------------+
|                                      | padding to max model input length | :obj:`tokenizer(batch_sentences, padding='max_length', truncation=True)` or                 |
|                                      |                                   | :obj:`tokenizer(batch_sentences, padding='max_length', truncation=STRATEGY)`                |
|                                      +-----------------------------------+---------------------------------------------------------------------------------------------+
|                                      | padding to specific length        | Not possible                                                                                |
+--------------------------------------+-----------------------------------+---------------------------------------------------------------------------------------------+
| truncation to specific length        | no padding                        | :obj:`tokenizer(batch_sentences, truncation=True, max_length=42)` or                        |
|                                      |                                   | :obj:`tokenizer(batch_sentences, truncation=STRATEGY, max_length=42)`                       |
|                                      +-----------------------------------+---------------------------------------------------------------------------------------------+
|                                      | padding to max sequence in batch  | :obj:`tokenizer(batch_sentences, padding=True, truncation=True, max_length=42)` or          |
|                                      |                                   | :obj:`tokenizer(batch_sentences, padding=True, truncation=STRATEGY, max_length=42)`         |
|                                      +-----------------------------------+---------------------------------------------------------------------------------------------+
|                                      | padding to max model input length | Not possible                                                                                |
|                                      +-----------------------------------+---------------------------------------------------------------------------------------------+
|                                      | padding to specific length        | :obj:`tokenizer(batch_sentences, padding='max_length', truncation=True, max_length=42)` or  |
|                                      |                                   | :obj:`tokenizer(batch_sentences, padding='max_length', truncation=STRATEGY, max_length=42)` |
+--------------------------------------+-----------------------------------+---------------------------------------------------------------------------------------------+

Pre-tokenized inputs
~~~~~~~~~~~~~~~~~~~~

The tokenizer also accept pre-tokenized inputs. This is particularly useful when you want to compute labels and extract
predictions in `named entity recognition (NER) <https://en.wikipedia.org/wiki/Named-entity_recognition>`__ or
`part-of-speech tagging (POS tagging) <https://en.wikipedia.org/wiki/Part-of-speech_tagging>`__.

Sylvain Gugger's avatar
Sylvain Gugger committed
287
288
289
290
291
292
.. warning::

    Pre-tokenized does not mean your inputs are already tokenized (you wouldn't need to pass them though the tokenizer
    if that was the case) but just split into words (which is often the first step in subword tokenization algorithms
    like BPE).

293
If you want to use pre-tokenized inputs, just set :obj:`is_split_into_words=True` when passing your inputs to the
Sylvain Gugger's avatar
Sylvain Gugger committed
294
tokenizer. For instance, we have:
Sylvain Gugger's avatar
Sylvain Gugger committed
295

Sylvain Gugger's avatar
Sylvain Gugger committed
296
.. code-block::
Sylvain Gugger's avatar
Sylvain Gugger committed
297

298
    >>> encoded_input = tokenizer(["Hello", "I'm", "a", "single", "sentence"], is_split_into_words=True)
Sylvain Gugger's avatar
Sylvain Gugger committed
299
    >>> print(encoded_input)
Sylvain Gugger's avatar
Sylvain Gugger committed
300
301
302
303
304
305
306
307
308
309
    {'input_ids': [101, 8667, 146, 112, 182, 170, 1423, 5650, 102],
     'token_type_ids': [0, 0, 0, 0, 0, 0, 0, 0, 0], 
     'attention_mask': [1, 1, 1, 1, 1, 1, 1, 1, 1]}

Note that the tokenizer still adds the ids of special tokens (if applicable) unless you pass
``add_special_tokens=False``.

This works exactly as before for batch of sentences or batch of pairs of sentences. You can encode a batch of sentences
like this:

Sylvain Gugger's avatar
Sylvain Gugger committed
310
.. code-block::
Sylvain Gugger's avatar
Sylvain Gugger committed
311
312
313
314

    batch_sentences = [["Hello", "I'm", "a", "single", "sentence"],
                       ["And", "another", "sentence"],
                       ["And", "the", "very", "very", "last", "one"]]
315
    encoded_inputs = tokenizer(batch_sentences, is_split_into_words=True)
Sylvain Gugger's avatar
Sylvain Gugger committed
316
317
318

or a batch of pair sentences like this:

Sylvain Gugger's avatar
Sylvain Gugger committed
319
.. code-block::
Sylvain Gugger's avatar
Sylvain Gugger committed
320
321
322
323

    batch_of_second_sentences = [["I'm", "a", "sentence", "that", "goes", "with", "the", "first", "sentence"],
                                 ["And", "I", "should", "be", "encoded", "with", "the", "second", "sentence"],
                                 ["And", "I", "go", "with", "the", "very", "last", "one"]]
324
    encoded_inputs = tokenizer(batch_sentences, batch_of_second_sentences, is_split_into_words=True)
Sylvain Gugger's avatar
Sylvain Gugger committed
325
326
327

And you can add padding, truncation as well as directly return tensors like before:

Sylvain Gugger's avatar
Sylvain Gugger committed
328
.. code-block::
Sylvain Gugger's avatar
Sylvain Gugger committed
329
330
331
332

    ## PYTORCH CODE
    batch = tokenizer(batch_sentences,
                      batch_of_second_sentences,
333
                      is_split_into_words=True,
Sylvain Gugger's avatar
Sylvain Gugger committed
334
335
336
337
338
339
                      padding=True,
                      truncation=True,
                      return_tensors="pt")
    ## TENSORFLOW CODE
    batch = tokenizer(batch_sentences,
                      batch_of_second_sentences,
340
                      is_split_into_words=True,
Sylvain Gugger's avatar
Sylvain Gugger committed
341
342
343
                      padding=True,
                      truncation=True,
                      return_tensors="tf")