quicktour.mdx 17.4 KB
Newer Older
Steven Liu's avatar
Steven Liu committed
1
<!--Copyright 2022 The HuggingFace Team. All rights reserved.
Sylvain Gugger's avatar
Sylvain Gugger committed
2
3
4
5
6
7
8
9
10
11
12
13
14

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
specific language governing permissions and limitations under the License.
-->

# Quick tour

15
16
[[open-in-colab]]

Steven Liu's avatar
Steven Liu committed
17
Get up and running with 馃 Transformers! Start using the [`pipeline`] for rapid inference, and quickly load a pretrained model and tokenizer with an [AutoClass](./model_doc/auto) to solve your text, vision or audio task.
Sylvain Gugger's avatar
Sylvain Gugger committed
18
19
20

<Tip>

Steven Liu's avatar
Steven Liu committed
21
22
All code examples presented in the documentation have a toggle on the top left for PyTorch and TensorFlow. If
not, the code is expected to work for both backends without any change.
Sylvain Gugger's avatar
Sylvain Gugger committed
23
24
25

</Tip>

Steven Liu's avatar
Steven Liu committed
26
## Pipeline
Sylvain Gugger's avatar
Sylvain Gugger committed
27

Steven Liu's avatar
Steven Liu committed
28
[`pipeline`] is the easiest way to use a pretrained model for a given task.
Sylvain Gugger's avatar
Sylvain Gugger committed
29
30
31

<Youtube id="tiZFewofSLM"/>

Steven Liu's avatar
Steven Liu committed
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
The [`pipeline`] supports many common tasks out-of-the-box:

**Text**:
* Sentiment analysis: classify the polarity of a given text.
* Text generation (in English): generate text from a given input.
* Name entity recognition (NER): label each word with the entity it represents (person, date, location, etc.).
* Question answering: extract the answer from the context, given some context and a question.
* Fill-mask: fill in the blank given a text with masked words.
* Summarization: generate a summary of a long sequence of text or document.
* Translation: translate text into another language.
* Feature extraction: create a tensor representation of the text.

**Image**:
* Image classification: classify an image.
* Image segmentation: classify every pixel in an image.
* Object detection: detect objects within an image.

**Audio**:
* Audio classification: assign a label to a given segment of audio.
* Automatic speech recognition (ASR): transcribe audio data into text.

<Tip>

For more details about the [`pipeline`] and associated tasks, refer to the documentation [here](./main_classes/pipelines).

</Tip>
Sylvain Gugger's avatar
Sylvain Gugger committed
58

Steven Liu's avatar
Steven Liu committed
59
### Pipeline usage
Sylvain Gugger's avatar
Sylvain Gugger committed
60

Steven Liu's avatar
Steven Liu committed
61
In the following example, you will use the [`pipeline`] for sentiment analysis.
Sylvain Gugger's avatar
Sylvain Gugger committed
62

Steven Liu's avatar
Steven Liu committed
63
Install the following dependencies if you haven't already:
Sylvain Gugger's avatar
Sylvain Gugger committed
64

Sylvain Gugger's avatar
Sylvain Gugger committed
65
66
<frameworkcontent>
<pt>
Sylvain Gugger's avatar
Sylvain Gugger committed
67
68
```bash
pip install torch
Sylvain Gugger's avatar
Sylvain Gugger committed
69
70
71
72
```
</pt>
<tf>
```bash
Sylvain Gugger's avatar
Sylvain Gugger committed
73
74
pip install tensorflow
```
Sylvain Gugger's avatar
Sylvain Gugger committed
75
76
</tf>
</frameworkcontent>
Sylvain Gugger's avatar
Sylvain Gugger committed
77

Steven Liu's avatar
Steven Liu committed
78
79
Import [`pipeline`] and specify the task you want to complete:

Sylvain Gugger's avatar
Sylvain Gugger committed
80
81
```py
>>> from transformers import pipeline
Sylvain Gugger's avatar
Sylvain Gugger committed
82
83

>>> classifier = pipeline("sentiment-analysis")
Sylvain Gugger's avatar
Sylvain Gugger committed
84
85
```

Steven Liu's avatar
Steven Liu committed
86
The pipeline downloads and caches a default [pretrained model](https://huggingface.co/distilbert-base-uncased-finetuned-sst-2-english) and tokenizer for sentiment analysis. Now you can use the `classifier` on your target text:
Sylvain Gugger's avatar
Sylvain Gugger committed
87
88

```py
Sylvain Gugger's avatar
Sylvain Gugger committed
89
>>> classifier("We are very happy to show you the 馃 Transformers library.")
90
[{'label': 'POSITIVE', 'score': 0.9998}]
Sylvain Gugger's avatar
Sylvain Gugger committed
91
92
```

Steven Liu's avatar
Steven Liu committed
93
For more than one sentence, pass a list of sentences to the [`pipeline`] which returns a list of dictionaries:
Sylvain Gugger's avatar
Sylvain Gugger committed
94
95

```py
Sylvain Gugger's avatar
Sylvain Gugger committed
96
>>> results = classifier(["We are very happy to show you the 馃 Transformers library.", "We hope you don't hate it."])
Sylvain Gugger's avatar
Sylvain Gugger committed
97
98
99
100
101
102
>>> for result in results:
...     print(f"label: {result['label']}, with score: {round(result['score'], 4)}")
label: POSITIVE, with score: 0.9998
label: NEGATIVE, with score: 0.5309
```

Steven Liu's avatar
Steven Liu committed
103
The [`pipeline`] can also iterate over an entire dataset. Start by installing the [馃 Datasets](https://huggingface.co/docs/datasets/) library:
Sylvain Gugger's avatar
Sylvain Gugger committed
104

Steven Liu's avatar
Steven Liu committed
105
106
107
```bash
pip install datasets 
```
Sylvain Gugger's avatar
Sylvain Gugger committed
108

109
Create a [`pipeline`] with the task you want to solve for and the model you want to use.
Sylvain Gugger's avatar
Sylvain Gugger committed
110
111

```py
112
>>> import torch
Steven Liu's avatar
Steven Liu committed
113
>>> from transformers import pipeline
Sylvain Gugger's avatar
Sylvain Gugger committed
114

115
>>> speech_recognizer = pipeline("automatic-speech-recognition", model="facebook/wav2vec2-base-960h")
Steven Liu's avatar
Steven Liu committed
116
```
Sylvain Gugger's avatar
Sylvain Gugger committed
117

118
Next, load a dataset (see the 馃 Datasets [Quick Start](https://huggingface.co/docs/datasets/quickstart.html) for more details) you'd like to iterate over. For example, let's load the [MInDS-14](https://huggingface.co/datasets/PolyAI/minds14) dataset:
Sylvain Gugger's avatar
Sylvain Gugger committed
119
120

```py
121
>>> from datasets import load_dataset, Audio
Steven Liu's avatar
Steven Liu committed
122

123
>>> dataset = load_dataset("PolyAI/minds14", name="en-US", split="train")  # doctest: +IGNORE_RESULT
Sylvain Gugger's avatar
Sylvain Gugger committed
124
125
```

126
127
We need to make sure that the sampling rate of the dataset matches the sampling 
rate `facebook/wav2vec2-base-960h` was trained on.
Sylvain Gugger's avatar
Sylvain Gugger committed
128
129

```py
130
131
132
133
134
135
136
>>> dataset = dataset.cast_column("audio", Audio(sampling_rate=speech_recognizer.feature_extractor.sampling_rate))
```

Audio files are automatically loaded and resampled when calling the `"audio"` column.
Let's extract the raw waveform arrays of the first 4 samples and pass it as a list to the pipeline:

```py
137
138
>>> result = speech_recognizer(dataset[:4]["audio"])
>>> print([d["text"] for d in result])
139
['I WOULD LIKE TO SET UP A JOINT ACCOUNT WITH MY PARTNER HOW DO I PROCEED WITH DOING THAT', "FODING HOW I'D SET UP A JOIN TO HET WITH MY WIFE AND WHERE THE AP MIGHT BE", "I I'D LIKE TOY SET UP A JOINT ACCOUNT WITH MY PARTNER I'M NOT SEEING THE OPTION TO DO IT ON THE AP SO I CALLED IN TO GET SOME HELP CAN I JUST DO IT OVER THE PHONE WITH YOU AND GIVE YOU THE INFORMATION OR SHOULD I DO IT IN THE AP AND I'M MISSING SOMETHING UQUETTE HAD PREFERRED TO JUST DO IT OVER THE PHONE OF POSSIBLE THINGS", 'HOW DO I THURN A JOIN A COUNT']
Steven Liu's avatar
Steven Liu committed
140
```
Sylvain Gugger's avatar
Sylvain Gugger committed
141

142
For a larger dataset where the inputs are big (like in speech or vision), you will want to pass along a generator instead of a list that loads all the inputs in memory. See the [pipeline documentation](./main_classes/pipelines) for more information.
143

Steven Liu's avatar
Steven Liu committed
144
### Use another model and tokenizer in the pipeline
Sylvain Gugger's avatar
Sylvain Gugger committed
145

Steven Liu's avatar
Steven Liu committed
146
The [`pipeline`] can accommodate any model from the [Model Hub](https://huggingface.co/models), making it easy to adapt the [`pipeline`] for other use-cases. For example, if you'd like a model capable of handling French text, use the tags on the Model Hub to filter for an appropriate model. The top filtered result returns a multilingual [BERT model](https://huggingface.co/nlptown/bert-base-multilingual-uncased-sentiment) fine-tuned for sentiment analysis. Great, let's use this model!
Sylvain Gugger's avatar
Sylvain Gugger committed
147

Steven Liu's avatar
Steven Liu committed
148
149
150
```py
>>> model_name = "nlptown/bert-base-multilingual-uncased-sentiment"
```
Sylvain Gugger's avatar
Sylvain Gugger committed
151

Sylvain Gugger's avatar
Sylvain Gugger committed
152
153
154
<frameworkcontent>
<pt>
Use the [`AutoModelForSequenceClassification`] and [`AutoTokenizer`] to load the pretrained model and it's associated tokenizer (more on an `AutoClass` below):
Sylvain Gugger's avatar
Sylvain Gugger committed
155
156
157

```py
>>> from transformers import AutoTokenizer, AutoModelForSequenceClassification
Sylvain Gugger's avatar
Sylvain Gugger committed
158

Steven Liu's avatar
Steven Liu committed
159
>>> model = AutoModelForSequenceClassification.from_pretrained(model_name)
Sylvain Gugger's avatar
Sylvain Gugger committed
160
>>> tokenizer = AutoTokenizer.from_pretrained(model_name)
Sylvain Gugger's avatar
Sylvain Gugger committed
161
162
163
164
165
166
```
</pt>
<tf>
Use the [`TFAutoModelForSequenceClassification`] and [`AutoTokenizer`] to load the pretrained model and it's associated tokenizer (more on an `TFAutoClass` below):

```py
Sylvain Gugger's avatar
Sylvain Gugger committed
167
>>> from transformers import AutoTokenizer, TFAutoModelForSequenceClassification
Sylvain Gugger's avatar
Sylvain Gugger committed
168

Steven Liu's avatar
Steven Liu committed
169
>>> model = TFAutoModelForSequenceClassification.from_pretrained(model_name)
Sylvain Gugger's avatar
Sylvain Gugger committed
170
171
>>> tokenizer = AutoTokenizer.from_pretrained(model_name)
```
Sylvain Gugger's avatar
Sylvain Gugger committed
172
173
</tf>
</frameworkcontent>
Sylvain Gugger's avatar
Sylvain Gugger committed
174

Steven Liu's avatar
Steven Liu committed
175
176
177
178
179
Then you can specify the model and tokenizer in the [`pipeline`], and apply the `classifier` on your target text:

```py
>>> classifier = pipeline("sentiment-analysis", model=model, tokenizer=tokenizer)
>>> classifier("Nous sommes tr猫s heureux de vous pr茅senter la biblioth猫que 馃 Transformers.")
180
[{'label': '5 stars', 'score': 0.7273}]
Steven Liu's avatar
Steven Liu committed
181
182
183
184
185
186
187
188
189
190
191
```

If you can't find a model for your use-case, you will need to fine-tune a pretrained model on your data. Take a look at our [fine-tuning tutorial](./training) to learn how. Finally, after you've fine-tuned your pretrained model, please consider sharing it (see tutorial [here](./model_sharing)) with the community on the Model Hub to democratize NLP for everyone! 馃

## AutoClass

<Youtube id="AhChOFRegn4"/>

Under the hood, the [`AutoModelForSequenceClassification`] and [`AutoTokenizer`] classes work together to power the [`pipeline`]. An [AutoClass](./model_doc/auto) is a shortcut that automatically retrieves the architecture of a pretrained model from it's name or path. You only need to select the appropriate `AutoClass` for your task and it's associated tokenizer with [`AutoTokenizer`]. 

Let's return to our example and see how you can use the `AutoClass` to replicate the results of the [`pipeline`].
Sylvain Gugger's avatar
Sylvain Gugger committed
192

Steven Liu's avatar
Steven Liu committed
193
### AutoTokenizer
Sylvain Gugger's avatar
Sylvain Gugger committed
194

Steven Liu's avatar
Steven Liu committed
195
A tokenizer is responsible for preprocessing text into a format that is understandable to the model. First, the tokenizer will split the text into words called *tokens*. There are multiple rules that govern the tokenization process, including how to split a word and at what level (learn more about tokenization [here](./tokenizer_summary)). The most important thing to remember though is you need to instantiate the tokenizer with the same model name to ensure you're using the same tokenization rules a model was pretrained with.
Sylvain Gugger's avatar
Sylvain Gugger committed
196

Steven Liu's avatar
Steven Liu committed
197
Load a tokenizer with [`AutoTokenizer`]:
Sylvain Gugger's avatar
Sylvain Gugger committed
198
199

```py
Steven Liu's avatar
Steven Liu committed
200
201
202
203
>>> from transformers import AutoTokenizer

>>> model_name = "nlptown/bert-base-multilingual-uncased-sentiment"
>>> tokenizer = AutoTokenizer.from_pretrained(model_name)
Sylvain Gugger's avatar
Sylvain Gugger committed
204
205
```

Steven Liu's avatar
Steven Liu committed
206
Next, the tokenizer converts the tokens into numbers in order to construct a tensor as input to the model. This is known as the model's *vocabulary*.
Sylvain Gugger's avatar
Sylvain Gugger committed
207

Steven Liu's avatar
Steven Liu committed
208
Pass your text to the tokenizer:
Sylvain Gugger's avatar
Sylvain Gugger committed
209
210

```py
Steven Liu's avatar
Steven Liu committed
211
212
>>> encoding = tokenizer("We are very happy to show you the 馃 Transformers library.")
>>> print(encoding)
213
214
215
{'input_ids': [101, 11312, 10320, 12495, 19308, 10114, 11391, 10855, 10103, 100, 58263, 13299, 119, 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
216
217
```

Steven Liu's avatar
Steven Liu committed
218
219
220
221
222
223
The tokenizer will return a dictionary containing:

* [input_ids](./glossary#input-ids): numerical representions of your tokens.
* [atttention_mask](.glossary#attention-mask): indicates which tokens should be attended to.

Just like the [`pipeline`], the tokenizer will accept a list of inputs. In addition, the tokenizer can also pad and truncate the text to return a batch with uniform length:
Sylvain Gugger's avatar
Sylvain Gugger committed
224

Sylvain Gugger's avatar
Sylvain Gugger committed
225
226
<frameworkcontent>
<pt>
Sylvain Gugger's avatar
Sylvain Gugger committed
227
228
229
230
231
232
```py
>>> pt_batch = tokenizer(
...     ["We are very happy to show you the 馃 Transformers library.", "We hope you don't hate it."],
...     padding=True,
...     truncation=True,
...     max_length=512,
Sylvain Gugger's avatar
Sylvain Gugger committed
233
...     return_tensors="pt",
Sylvain Gugger's avatar
Sylvain Gugger committed
234
... )
Sylvain Gugger's avatar
Sylvain Gugger committed
235
236
237
238
```
</pt>
<tf>
```py
Sylvain Gugger's avatar
Sylvain Gugger committed
239
240
241
242
243
>>> tf_batch = tokenizer(
...     ["We are very happy to show you the 馃 Transformers library.", "We hope you don't hate it."],
...     padding=True,
...     truncation=True,
...     max_length=512,
Sylvain Gugger's avatar
Sylvain Gugger committed
244
...     return_tensors="tf",
Sylvain Gugger's avatar
Sylvain Gugger committed
245
246
... )
```
Sylvain Gugger's avatar
Sylvain Gugger committed
247
248
</tf>
</frameworkcontent>
Sylvain Gugger's avatar
Sylvain Gugger committed
249

Steven Liu's avatar
Steven Liu committed
250
Read the [preprocessing](./preprocessing) tutorial for more details about tokenization.
Sylvain Gugger's avatar
Sylvain Gugger committed
251

Steven Liu's avatar
Steven Liu committed
252
### AutoModel
Sylvain Gugger's avatar
Sylvain Gugger committed
253

Sylvain Gugger's avatar
Sylvain Gugger committed
254
255
256
<frameworkcontent>
<pt>
馃 Transformers provides a simple and unified way to load pretrained instances. This means you can load an [`AutoModel`] like you would load an [`AutoTokenizer`]. The only difference is selecting the correct [`AutoModel`] for the task. Since you are doing text - or sequence - classification, load [`AutoModelForSequenceClassification`]:
Sylvain Gugger's avatar
Sylvain Gugger committed
257
258

```py
Steven Liu's avatar
Steven Liu committed
259
>>> from transformers import AutoModelForSequenceClassification
Sylvain Gugger's avatar
Sylvain Gugger committed
260

Steven Liu's avatar
Steven Liu committed
261
262
263
>>> model_name = "nlptown/bert-base-multilingual-uncased-sentiment"
>>> pt_model = AutoModelForSequenceClassification.from_pretrained(model_name)
```
Sylvain Gugger's avatar
Sylvain Gugger committed
264
265
266

<Tip>

Steven Liu's avatar
Steven Liu committed
267
See the [task summary](./task_summary) for which [`AutoModel`] class to use for which task.
Sylvain Gugger's avatar
Sylvain Gugger committed
268
269
270

</Tip>

Sylvain Gugger's avatar
Sylvain Gugger committed
271
Now you can pass your preprocessed batch of inputs directly to the model. You just have to unpack the dictionary by adding `**`:
Sylvain Gugger's avatar
Sylvain Gugger committed
272
273

```py
Steven Liu's avatar
Steven Liu committed
274
>>> pt_outputs = pt_model(**pt_batch)
Sylvain Gugger's avatar
Sylvain Gugger committed
275
276
```

Steven Liu's avatar
Steven Liu committed
277
The model outputs the final activations in the `logits` attribute. Apply the softmax function to the `logits` to retrieve the probabilities:
Sylvain Gugger's avatar
Sylvain Gugger committed
278
279

```py
Steven Liu's avatar
Steven Liu committed
280
281
282
>>> from torch import nn

>>> pt_predictions = nn.functional.softmax(pt_outputs.logits, dim=-1)
Sylvain Gugger's avatar
Sylvain Gugger committed
283
>>> print(pt_predictions)
284
285
tensor([[0.0021, 0.0018, 0.0115, 0.2121, 0.7725],
        [0.2084, 0.1826, 0.1969, 0.1755, 0.2365]], grad_fn=<SoftmaxBackward0>)
Sylvain Gugger's avatar
Sylvain Gugger committed
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
```
</pt>
<tf>
馃 Transformers provides a simple and unified way to load pretrained instances. This means you can load an [`TFAutoModel`] like you would load an [`AutoTokenizer`]. The only difference is selecting the correct [`TFAutoModel`] for the task. Since you are doing text - or sequence - classification, load [`TFAutoModelForSequenceClassification`]:

```py
>>> from transformers import TFAutoModelForSequenceClassification

>>> model_name = "nlptown/bert-base-multilingual-uncased-sentiment"
>>> tf_model = TFAutoModelForSequenceClassification.from_pretrained(model_name)
```

<Tip>

See the [task summary](./task_summary) for which [`AutoModel`] class to use for which task.
301

Sylvain Gugger's avatar
Sylvain Gugger committed
302
303
304
305
306
307
308
309
310
311
312
</Tip>

Now you can pass your preprocessed batch of inputs directly to the model by passing the dictionary keys directly to the tensors:

```py
>>> tf_outputs = tf_model(tf_batch)
```

The model outputs the final activations in the `logits` attribute. Apply the softmax function to the `logits` to retrieve the probabilities:

```py
Steven Liu's avatar
Steven Liu committed
313
314
315
>>> import tensorflow as tf

>>> tf_predictions = tf.nn.softmax(tf_outputs.logits, axis=-1)
316
>>> tf_predictions  # doctest: +IGNORE_RESULT
Sylvain Gugger's avatar
Sylvain Gugger committed
317
```
Sylvain Gugger's avatar
Sylvain Gugger committed
318
319
</tf>
</frameworkcontent>
Sylvain Gugger's avatar
Sylvain Gugger committed
320

Steven Liu's avatar
Steven Liu committed
321
<Tip>
Sylvain Gugger's avatar
Sylvain Gugger committed
322

Steven Liu's avatar
Steven Liu committed
323
324
All 馃 Transformers models (PyTorch or TensorFlow) outputs the tensors *before* the final activation
function (like softmax) because the final activation function is often fused with the loss.
Sylvain Gugger's avatar
Sylvain Gugger committed
325

Steven Liu's avatar
Steven Liu committed
326
</Tip>
Sylvain Gugger's avatar
Sylvain Gugger committed
327

Steven Liu's avatar
Steven Liu committed
328
Models are a standard [`torch.nn.Module`](https://pytorch.org/docs/stable/nn.html#torch.nn.Module) or a [`tf.keras.Model`](https://www.tensorflow.org/api_docs/python/tf/keras/Model) so you can use them in your usual training loop. However, to make things easier, 馃 Transformers provides a [`Trainer`] class for PyTorch that adds functionality for distributed training, mixed precision, and more. For TensorFlow, you can use the `fit` method from [Keras](https://keras.io/). Refer to the [training tutorial](./training) for more details.
Sylvain Gugger's avatar
Sylvain Gugger committed
329
330
331

<Tip>

Steven Liu's avatar
Steven Liu committed
332
333
馃 Transformers model outputs are special dataclasses so their attributes are autocompleted in an IDE.
The model outputs also behave like a tuple or a dictionary (e.g., you can index with an integer, a slice or a string) in which case the attributes that are `None` are ignored.
Sylvain Gugger's avatar
Sylvain Gugger committed
334
335
336

</Tip>

Steven Liu's avatar
Steven Liu committed
337
338
### Save a model

Sylvain Gugger's avatar
Sylvain Gugger committed
339
340
<frameworkcontent>
<pt>
Steven Liu's avatar
Steven Liu committed
341
Once your model is fine-tuned, you can save it with its tokenizer using [`PreTrainedModel.save_pretrained`]:
Sylvain Gugger's avatar
Sylvain Gugger committed
342
343

```py
Sylvain Gugger's avatar
Sylvain Gugger committed
344
>>> pt_save_directory = "./pt_save_pretrained"
345
>>> tokenizer.save_pretrained(pt_save_directory)  # doctest: +IGNORE_RESULT
Sylvain Gugger's avatar
Sylvain Gugger committed
346
>>> pt_model.save_pretrained(pt_save_directory)
Sylvain Gugger's avatar
Sylvain Gugger committed
347
348
349
350
351
352
353
354
355
356
357
358
```

When you are ready to use the model again, reload it with [`PreTrainedModel.from_pretrained`]:

```py
>>> pt_model = AutoModelForSequenceClassification.from_pretrained("./pt_save_pretrained")
```
</pt>
<tf>
Once your model is fine-tuned, you can save it with its tokenizer using [`TFPreTrainedModel.save_pretrained`]:

```py
Sylvain Gugger's avatar
Sylvain Gugger committed
359
>>> tf_save_directory = "./tf_save_pretrained"
360
>>> tokenizer.save_pretrained(tf_save_directory)  # doctest: +IGNORE_RESULT
Sylvain Gugger's avatar
Sylvain Gugger committed
361
362
363
>>> tf_model.save_pretrained(tf_save_directory)
```

Sylvain Gugger's avatar
Sylvain Gugger committed
364
When you are ready to use the model again, reload it with [`TFPreTrainedModel.from_pretrained`]:
Sylvain Gugger's avatar
Sylvain Gugger committed
365

Steven Liu's avatar
Steven Liu committed
366
367
```py
>>> tf_model = TFAutoModelForSequenceClassification.from_pretrained("./tf_save_pretrained")
Sylvain Gugger's avatar
Sylvain Gugger committed
368
```
Sylvain Gugger's avatar
Sylvain Gugger committed
369
370
</tf>
</frameworkcontent>
Sylvain Gugger's avatar
Sylvain Gugger committed
371

Steven Liu's avatar
Steven Liu committed
372
One particularly cool 馃 Transformers feature is the ability to save a model and reload it as either a PyTorch or TensorFlow model. The `from_pt` or `from_tf` parameter can convert the model from one framework to the other:
Sylvain Gugger's avatar
Sylvain Gugger committed
373

Sylvain Gugger's avatar
Sylvain Gugger committed
374
375
<frameworkcontent>
<pt>
Sylvain Gugger's avatar
Sylvain Gugger committed
376
377
```py
>>> from transformers import AutoModel
Sylvain Gugger's avatar
Sylvain Gugger committed
378

Sylvain Gugger's avatar
Sylvain Gugger committed
379
>>> tokenizer = AutoTokenizer.from_pretrained(tf_save_directory)
Steven Liu's avatar
Steven Liu committed
380
>>> pt_model = AutoModelForSequenceClassification.from_pretrained(tf_save_directory, from_tf=True)
Sylvain Gugger's avatar
Sylvain Gugger committed
381
382
383
384
```
</pt>
<tf>
```py
385
>>> from transformers import TFAutoModel
Sylvain Gugger's avatar
Sylvain Gugger committed
386

387
>>> tokenizer = AutoTokenizer.from_pretrained(pt_save_directory)
Steven Liu's avatar
Steven Liu committed
388
>>> tf_model = TFAutoModelForSequenceClassification.from_pretrained(pt_save_directory, from_pt=True)
389
```
Sylvain Gugger's avatar
Sylvain Gugger committed
390
391
</tf>
</frameworkcontent>
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430

## Custom model builds

You can modify the model's configuration class to change how a model is built. The configuration specifies a model's attributes, such as the number of hidden layers or attention heads. You start from scratch when you initialize a model from a custom configuration class. The model attributes are randomly initialized, and you'll need to train the model before you can use it to get meaningful results.

Start by importing [`AutoConfig`], and then load the pretrained model you want to modify. Within [`AutoConfig.from_pretrained`], you can specify the attribute you want to change, such as the number of attention heads:

```py
>>> from transformers import AutoConfig

>>> my_config = AutoConfig.from_pretrained("distilbert-base-uncased", n_heads=12)
```

<frameworkcontent>
<pt>
Create a model from your custom configuration with [`AutoModel.from_config`]:

```py
>>> from transformers import AutoModel

>>> my_model = AutoModel.from_config(my_config)
```
</pt>
<tf>
Create a model from your custom configuration with [`TFAutoModel.from_config`]:

```py
>>> from transformers import TFAutoModel

>>> my_model = TFAutoModel.from_config(my_config)
```
</tf>
</frameworkcontent>

Take a look at the [Create a custom architecture](./create_a_model) guide for more information about building custom configurations.

## What's next?

Now that you've completed the 馃 Transformers quick tour, check out our guides and learn how to do more specific things like writing a custom model, fine-tuning a model for a task, and how to train a model with a script. If you're interested in learning more about 馃 Transformers core concepts, grab a cup of coffee and take a look at our Conceptual Guides!